Say that I have the following interface:
public interface IUserListVM
{
IList<string> UserList { get; }
}
and a view model implementing this view model interface:
public class UserListVM : IUserListVM
{
private IList<string> userList = new List<string>();
IList<string> UserList
{
get { return userList; }
}
}
is it then possible to have a view that expects a model that inherits the IUserListVM. Say that I have a UserList partial view that looks something like this:
@model MVCWebsite.Views.IUserListVM
@foreach (string user in Model.UserList) {
user
}
The meaning of this is to have partial views as standalone as possible.
In MVC we cannot pass multiple models from a controller to the single view.
You can use multiple models in a single view by creating a common model for all the models that are to be used in a single view. To achieve this, refer to the following steps. First, create a new model (common for all models) and refer all other models that are to be used in the same view.
You can have methods in your ViewModel .
The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.
After your edit - yes that is entirely possible. See dotnetfiddle
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