how to create a view with multiple models mvc 4?

后端 未结 2 1292
一整个雨季
一整个雨季 2021-01-14 09:00

so i works in asp.net mvc 4 project and i have a problem in my view, what i want is create a view with 2 differnt type of model,first view (Index) take IEnumerable (Models.m

2条回答
  •  长情又很酷
    2021-01-14 09:53

    Create a new, compound view model that includes both of these view models.

    public class CompoundViewModel
    {
        public IEnumerable AllSubscribers {get; set;} 
        public SubscribersModel SelectedSubscriber {get; set;}
    }
    

    Ideally also split your view into partial views and render these two part of your compound model into them using DisplayFor<> or EditorFor<>. That way you can reuse the view for a 'SubscriberModel' elsewhere in the application if you need it.

    Your controller code could also be improved by using a dependency injection framework (e.g. Autofac) to inject those dependencies that you are currently newing up.

    Another alternative, given that you are using the same model for the list and the details view, would be to handle this client-side using Javascript, either manually using jQuery or with one of the newer frameworks that allows client-side model binding like Knockout.js or Angular.js.

提交回复
热议问题