模式的共同传递数据MVC4.5到_layout.cshtml [英] Pattern for passing common data to _layout.cshtml in MVC4.5
问题描述
我想拿出最好的方式将数据传递到我的_layout.cshtml页面。
我与创建一个共同的基类,所有查看具体型号衍生玩弄。这个基类会被我的_layout.cshtml识别并用于填充关于用户详细信息并在标题等。例如加载正确的图像,这里是它的一个片段。
公共抽象类ViewModelBase
{
公共字符串用户名{获得;组; }
公共字符串版本{搞定;组; }
}
在我_layout.cshtml顶部我有...
@model MyProject.Web.Controllers.ViewModelBase
我需要一个公共区域来滋润模型所需的信息,我打算使用下面的模式...
- 每个操作方法创建和水合物来源于一个模型
ViewModelBase。 - 的动作完成。
- 我创建了一个ActionFilterAttribute和覆盖OnActionExecuted铸就
目前的结果为ViewModelBase。 - 如果转换成功,那么我填充ViewModelBase细节与相关数据。
下面是我的问题...
- 是使用ActionFilterAttribute(OnActionExecuted)的良好格局为我所试图做的?
- 我不能看到如何让从HttpActionExecutedContext动作产生的结果。如何做到这一点?
我按照同样的方法,并使用一个基视图模型类,我的所有其他的ViewModels继承。
然后,我有一个基本的控制器,所有的控制器继承。在那里,我有一个方法,它初始化视图模型的护理:
保护牛逼CreateViewModel< T>()其中T:ViewModel.BaseViewModel,新的()
{
VAR viewModelT =新款T {
HeaderTitle =欢迎来到我的域名,
VisitorUsername = this.VisitorUsername,
IsCurrentVisitorAuthenticated = this.IsCurrentVisitorAuthenticated, // ...
}; 返回viewModelT;
}
然后每个控制器上,当我想创建视图模型,我只是调用基控制器的方法:
VAR VM = base.CreateViewModel< MyPageCustomViewModel>();
I am trying to come up with the best pattern for passing data to my _layout.cshtml page.
I am toying with creating a common base class from which all view specific models derive. This base class would be recognized by my _layout.cshtml and used to fill in details about the user and load proper images in the header, etc. For example, here is a snippet of it.
public abstract class ViewModelBase
{
public string Username { get; set; }
public string Version { get; set; }
}
At the top of my _layout.cshtml I have...
@model MyProject.Web.Controllers.ViewModelBase
I need a common area to hydrate the information required by the model, and am planning to use the following pattern...
- Each action method creates and hydrates a model derived from ViewModelBase.
- The action completes.
- I create a ActionFilterAttribute and override OnActionExecuted to cast the current Result to ViewModelBase.
- If the conversion is successful, then I populate the ViewModelBase details with the relevant data.
Here are my questions...
- Is the use of a ActionFilterAttribute (OnActionExecuted) a good pattern for what I am trying to do?
- I am not able to see how to get the Result created in the action from the HttpActionExecutedContext. How is this done?
I follow the same approach and use a base ViewModel class which all my other viewModels inherit from.
Then, I have a base controller that all controller inherit from. In there, I have one method that takes care of initializing the view model:
protected T CreateViewModel<T>() where T : ViewModel.BaseViewModel, new()
{
var viewModelT = new T {
HeaderTitle = "Welcome to my domain",
VisitorUsername = this.VisitorUsername,
IsCurrentVisitorAuthenticated = this.IsCurrentVisitorAuthenticated,
//...
};
return viewModelT;
}
Then on each controller, when I want to create the view model, I simply call the base controller's method:
var vm = base.CreateViewModel<MyPageCustomViewModel>();
这篇关于模式的共同传递数据MVC4.5到_layout.cshtml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!