asp.net mvc view with masterpage binding with different models

↘锁芯ラ 提交于 2020-01-02 11:03:25

问题


if i have a master page that binds with ObjectA and then a View that binds with ObjectB, how does that work (or does it work at all) in asp.net mvc.

master page might have:

Inherits="System.Web.Mvc.ViewMasterPage<CalendarEvent[]>" %>

and one of the view might have:

Inherits="System.Web.Mvc.ViewPage<Tournament[]>" %>

what would you pass into the view from the controller since there are 2 models in this case you are binding to?

is this bad practice to have the master page have a binding object?


回答1:


Well, you could define an abstract container that contains ObjectA:

public class ModelContainer
{
    public ObjectA ObjectA { get; set; }
}

and then have all your views inherit from this class and add their own data:

public class SomeViewContainer : ModelContainer
{
    public ObjectB ObjectB { get; set; }
}

The master page can then access the ObjectA property of the model, while the individual views can ignore that particular property and access the data they themselves need.

I can't say I particularly like this approach, though. If there was any way I could avoid needing a model in the master page, I'd rather prefer that.



来源:https://stackoverflow.com/questions/1320516/asp-net-mvc-view-with-masterpage-binding-with-different-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!