Why can't my view's model bind with my gen

2019-02-22 03:48发布

问题:

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>

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

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

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:

The model item passed into the dictionary is of type 'Project.ViewModels.CompanyViewModels.CompanyListViewModel`1[Project.ViewModels.CompanyViewModels.ICompanyInListAsUser]', but this dictionary requires a model item of type 'Project.ViewModels.CompanyViewModels.CompanyListViewModel`1[Project.ViewModels.CompanyViewModels.ICompany]'.

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

Any assistance would be greatly appreciated.

Thanks.

EDIT

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.

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.

回答1:

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.



回答2:

Consider that interfaces cannot provide implementations when you think about this situation and it should become apparent that you need to close your generic type with the specific interface you want the generic class to operate upon.

If it's not apparent, well, consider further that from a pure OOP standpoint, interfaces can't define implementations, only classes can, and that C# doesn't allow you to specify overlapping interfaces.

It sounds to me like you are trying to treat an interface like an abstract base class.