ASP.NET MVC Is it possible to have an interface as a model?

Say I have the following interface:

public interface IUserListVM { IList<string> UserList { get; } } 

and a view model that implements the interface of this model:

 public class UserListVM : IUserListVM { private IList<string> userList = new List<string>(); IList<string> UserList { get { return userList; } } } 

then it is possible to have a view that expects a model that inherits IUserListVM. Let's say that I have a partial UserList view that looks something like this:

  @model MVCWebsite.Views.IUserListVM @foreach (string user in Model.UserList) { user } 

The point of this is to have partial representations as autonomous as possible.

+5
source share
1 answer

After your editing - yes, it is quite possible. See dotnetfiddle

+4
source

Source: https://habr.com/ru/post/1207757/


All Articles