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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With