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.