ASP.NET MVC路过强类型数据掌握页 [英] ASP.NET MVC passing strongly typed data to master page
问题描述
<一个href=\"http://stackoverflow.com/questions/467855/should-an-asp-net-masterpage-get-its-data-from-the-view\">Should一个ASP.NET母版从视图中的相关数据?
我一直在关注,共同将数据传递到的Site.Master此方法。然而,这并不需要的ViewData的具体铸造和我不喜欢使用字符串标识符无处不在。这是做的最好的办法还是有另一种方式?
I have been following this method for passing common data to the site.master. However, this does require specific casting of the ViewData and I don't like using string identifiers everywhere. Is this the best way to do it or is there another way?
<一个href=\"http://www.asp.net/learn/MVC/tutorial-13-cs.aspx\">http://www.asp.net/learn/MVC/tutorial-13-cs.aspx
感谢
推荐答案
您可以创建一个基类,所有的车型继承:
You could create a base class that all your models inherit from:
class MasterModel {
// common info, used in master page.
}
class Page1Model : MasterModel {
// page 1 model
}
那么你的母版页会继承 ViewMasterPage&LT; MasterModel&GT;
和您的 Page1.aspx的
会从<$继承C $ C>的ViewPage&LT; Page1Model&GT; 并设置 Site.master母
作为它的母版页
Then your master page would inherit from ViewMasterPage<MasterModel>
and your Page1.aspx
would inherit from ViewPage<Page1Model>
and set Site.master
as its master page.
这篇关于ASP.NET MVC路过强类型数据掌握页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!