Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't my view's model bind with my generic ViewModel which implements an interface? (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>

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.

like image 380
cjones26 Avatar asked Apr 23 '11 05:04

cjones26


People also ask

How do you use a model in razor view?

Right-click in the Store Index action method and select Add View as before, select Genre as the Model class, and press the Add button. This tells the Razor view engine that it will be working with a model object that can hold several Genre objects.

What is the purpose of ViewModel in MVC?

In ASP.NET MVC, ViewModel is a class that contains the fields which are represented in the strongly-typed view. It is used to pass data from controller to strongly-typed view.

What does @model represent in razor view?

The @Model will contain all the data of the current page. So when you access a page on your site you can get data from that page with using the @Model.


2 Answers

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.

like image 168
chobojordi Avatar answered Sep 21 '22 02:09

chobojordi


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.

like image 26
gazarsgo Avatar answered Sep 19 '22 02:09

gazarsgo