为什么不能用我的,它实现一个接口通用视图模型我的观点的模型绑定? (ASP.NET MVC 3) [英] Why can't my view's model bind with my generic ViewModel which implements an interface? (ASP.NET MVC 3)

查看:63
本文介绍了为什么不能用我的,它实现一个接口通用视图模型我的观点的模型绑定? (ASP.NET MVC 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过我的查看以下视图模型的实例:

I'm trying to pass my View an instance of the following ViewModel:

public class CompanyListViewModel<T> where T : ICompany
{
    public IEnumerable<T> CompanyList;
    public IEnumerable<string> StateList;

    public CompanyListViewModel(IEnumerable<T> list)
    {
        CompanyList = list;
    }
}

在哪里查看需要的东西,像这样:

Where the View takes something like so:

@model Project.ViewModels.CompanyViewModels.CompanyListViewModel<ICompany>

和我的控制器code通过像这样的观点:

And my controller code passes something like this to the View:

CompanyListViewModel<ICompanyInListAsUser> model = new CompanyListViewModel<ICompanyInListAsUser>(_companyService.FilterByCompanyState(state));

ICompanyInListAsUser 接口实现的 ICompany 接口。在 _companyService.FilterByCompanyState(州))类返回公司的的IEnumerable 的对象,这反过来,落实 ICompanyInListAsUser 接口。

Where the ICompanyInListAsUser interface implements the ICompany interface. The _companyService.FilterByCompanyState(state)) class returns an IEnumerable of Company objects, which in turn, implement the ICompanyInListAsUser interface.

由于某些原因,我的访问时查看我收到以下错误:

For some reason, I receive the following error when accessing my View:

传递到字典中的模型产品类型'Project.ViewModels.CompanyViewModels.CompanyListViewModel`1[Project.ViewModels.CompanyViewModels.ICompanyInListAsUser]',但本词典需要类型的模型项目'Project.ViewModels.CompanyViewModels.CompanyListViewModel`1[Project.ViewModels.CompanyViewModels.ICompany]'.

为什么我收到此错误,如果 ICompanyInListAsUser 接口的确实的实际上是落实 ICompany

Why am I receiving this error if the ICompanyInListAsUser interface does in fact implement ICompany?

任何援助将大大AP preciated。

Any assistance would be greatly appreciated.

感谢。

修改

我也想说明什么,我想在这里实现。说我有我的应用程序(例如,用户和管理员)几个访问级别。如果我有我的公司对象的属性是仅管理员?那么在这种情况下,我的 ICompanyInListAsUser 将包括我想为列在列表中的除了的管理员,唯一的财产的所有属性。

I also wanted to state what I'm trying to achieve here. Say I have several access levels in my application (ex. User and Admin). What if I have a property in my Company object that is admin-only? Well in that case, my ICompanyInListAsUser would include all properties I wanted as columns in the list except the admin-only property.

所以,在这里我的计划是在传递作为视图模型的基地的IEnumerable 公司接口和控制器(或服务层)选择性地填充的IEnumerable 像这些过滤器的接口之一的实例 ICompanyInListAsUser

So my plan here was to pass in as the ViewModel an IEnumerable of base company interface and from the controller (or service layer) selectively populate the IEnumerable with an instance of one of these "filter" interfaces like ICompanyInListAsUser.

希望这是有道理的,请让我知道,如果我需要详细说明。

Hopefully this makes sense, please let me know if I need to elaborate.

推荐答案

你想实现应该通过使用视图模型类的接口和声明泛型类型的协变是可能的。像这样的:

What you're trying to accomplish should be possible by using an Interface for the ViewModel class and declaring the Generic type as Covariant. Like this:

interface ICompanyListViewModel<out T> where T : ICompany
{
}

这样,你也可以用一个抽象类,而不是为公司的接口脱身。

That way you could also get away with an abstract class instead of an interface for Company.

这篇关于为什么不能用我的,它实现一个接口通用视图模型我的观点的模型绑定? (ASP.NET MVC 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆