重复
我一直在使用这种方法来将公共数据传递给site.master。但是,这确实需要对ViewData进行特定的强制类型转换,而且我不喜欢到处都使用字符串标识符。这是最好的方法吗?还是有其他方法?
http://www.asp.net/learn/MVC/tutorial-13-cs.aspx
谢谢
我一直在使用这种方法来将公共数据传递给site.master。但是,这确实需要对ViewData进行特定的强制类型转换,而且我不喜欢到处都使用字符串标识符。这是最好的方法吗?还是有其他方法?
http://www.asp.net/learn/MVC/tutorial-13-cs.aspx
谢谢
class MasterModel {
// common info, used in master page.
}
class Page1Model : MasterModel {
// page 1 model
}
然后你的主页面将继承自 ViewMasterPage<MasterModel>
,而你的 Page1.aspx
将继承自 ViewPage<Page1Model>
并将 Site.master
设置为其主页面。
在我做了一些努力后,这是我的贡献:
class ModelBase {
// common info, used in master page.
}
class Page1Model : ModelBase {
// page 1 model
}
public class ControllerBase : Controller
{
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
var model = ViewData.Model as ModelBase;
// set common data here
base.OnActionExecuted(filterContext);
}
}