Why can't my view's model bind with my generic ViewModel which implements an interface? (ASP.NET MVC 3)
Asked Answered
G

2

6

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.

Gunshy answered 23/4, 2011 at 5:3 Comment(3)
You're not giving enough information. What is ICompanyInListAsUser?Stavro
Using interfaces on view models is a very unusual pattern I think. @gazarsgo is probably right that you should be using an abstract base class.Mumbletypeg
Chad, ICompanyInListAsUser is simply an interface containing the properties which I want to filter from my domain object. Ryan, could you please provide a quick example of exactly where I'd use the abstract base class? Are you saying create a abstract base class from where all my "filter" interfaces and base Company object derive? From here, make my ViewModel IEnumerable a list of this abstract base class? Thanks!Gunshy
S
6

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.

Stotts answered 25/4, 2011 at 21:19 Comment(0)
L
1

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.

Lucais answered 23/4, 2011 at 5:47 Comment(2)
I see what you're saying, just having a hard time putting it in context in this situation.Gunshy
as simple as changing CompanyListViewModel<ICompanyInListAsUser> model = new CompanyListViewModel<ICompanyInListAsUser>(_companyService.FilterByCompanyState(state)); to CompanyListViewModel<ICompany> model = new CompanyListViewModel<ICompany>(_companyService.FilterByCompanyState(state));Lucais

© 2022 - 2024 — McMap. All rights reserved.