与不同型号的母版结合asp.net mvc的观点 [英] asp.net mvc view with masterpage binding with different models

查看:118
本文介绍了与不同型号的母版结合asp.net mvc的观点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个母版页与对象A结合,然后与对象B结合视图,请问是怎么工作(或它在所有的工作)在asp.net的MVC。

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.

母版页可能有:

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

和视图的人们可能有:

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

你会传递到从控制器的看法,因为有2个型号在这种情况下要绑定到?

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?

推荐答案

那么,你的可能的定义包含对象A的抽象容器:

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; }
}

母版页可以再访问模型的对象A的财产,而个别意见,可以忽略特定的属性和访问他们自己需要的数据。

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.

我不能说我特别喜欢这种方法,虽然。如果有什么办法能够避免需要在母版页的模型,我宁愿preFER这一点。

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.

这篇关于与不同型号的母版结合asp.net mvc的观点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆