[英]How can I use and populate a base view model in a master page?
I have an asp.net masterpage, and All my controllers inherit from a controller base. 我有一个asp.net主页,所有我的控制器都继承自控制器库。 All my view models inherit from ViewBase.
我所有的视图模型都继承自ViewBase。 How can I have a base set of data in the master page that is populated from the base controller into the viewbase, then into the masterpage?
我如何在母版页中有一组基本数据,这些数据从基本控制器填充到视图库中,然后填充到母版页中?
What I have done in the past is used ViewData to populate my master page. 我过去所做的是使用ViewData填充我的母版页。
Inside your master page you can put: 在您的母版页中,您可以放置:
<% var baseModel = ViewData["baseModel"] as BaseViewModel; %>
then 然后
baseModel.xx
for whatever properties you need throughout your masterpage. baseModel.xx
用于整个baseModel.xx
所需的任何属性。
In my ControllerBase, I then override OnActionExecuting and popluate the viewData with an instance of my baseViewModel. 然后,在我的ControllerBase中,重写OnActionExecuting,并用baseViewModel的实例填充viewData。
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
if (filterContext.Canceled || filterContext.Exception != null)
return;
var viewResult = filterContext.Result as ViewResult;
var viewModel = new BaseViewModel();
PopulateBaseViewModel(viewModel);
viewResult.ViewData["baseModel"] = viewModel;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.