ASP.NET MVC 3布局所有子视图中的ViewBag数据

时间:2021-02-09 03:27:30

We need dynamic data passed to our layout file, no matter what the child view is. For example, we display some user specific data in the header of the layout.

无论子视图是什么,我们都需要传递给布局文件的动态数据。例如,我们在布局的标题中显示一些用户特定的数据。

How can we pass this data to the layout view without each action having to supply it independently? Should we use a custom controller, or is there a better solution?

如果每个动作都不必独立提供,我们如何将这些数据传递给布局视图?我们应该使用自定义控制器,还是有更好的解决方案?

4 个解决方案

#1


9  

The strategy that I use is to have a base view model from which all of my view models derive. I use a base controller, though you could also use a global filter, and override OnActionExecuted. When I detect an action that returns a ViewResult, I cast the model to my base view model and set the common properties on the model from the base controller.

我使用的策略是拥有一个基本视图模型,我的所有视图模型都是从该模型中派生出来的。我使用基本控制器,但您也可以使用全局过滤器,并覆盖OnActionExecuted。当我检测到返回ViewResult的动作时,我将模型转换为基本视图模型,并从基本控制器设置模型的公共属性。

The choice between a global filter and a base controller depends on a variety of factors. If it really applies to all actions (that return view results) and you don't need injection to get access to some resources, then I'd probably go with the filter. If you need to have dependencies injected or you have some controllers where the data would be applied and others where it wouldn't (say the Admin controller), then I'd go the base controller route. You will need to remember to derive from the controller if you go with it.

全局过滤器和基本控制器之间的选择取决于多种因素。如果它确实适用于所有操作(返回视图结果)并且您不需要注入来访问某些资源,那么我可能会使用过滤器。如果您需要注入依赖项,或者您有一些控制器将应用数据,而其他控制器则不应用(例如Admin控制器),那么我将使用基本控制器路由。如果你使用它,你需要记住从控制器派生。

You could also do the same thing with the ViewBag if you don't want to derive from a common view model. I like having the strongly-typed model, but YMMV.

如果您不想从公共视图模型派生,也可以使用ViewBag执行相同的操作。我喜欢强类型模型,但YMMV。

#2


7  

You could use @Html.Action("ActionName", "ControllerName") in Your _layout file.

您可以在_layout文件中使用@ Html.Action(“ActionName”,“ControllerName”)。

Here is article about this: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

以下是关于此的文章:http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

#3


2  

Take a look at the login controls that are standard in a Razor project - these partial views access user data - probably exactly like you'd like to.

看看Razor项目中标准的登录控件 - 这些部分视图访问用户数据 - 可能与您想要的完全一样。

e.g. a typical LogonPartial.cshtml might contain:

例如典型的LogonPartial.cshtml可能包含:

@if(Request.IsAuthenticated) {
    <text>Welcome <b>@Context.User.Identity.Name</b>!
    [ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
}
else {
    @:[ @Html.ActionLink("Log On", "LogOn", "Account") ]
}

#4


1  

I believe the core to your concerns would best be served by using Sections.

我相信使用Sections可以最好地满足您关注的核心。

http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

You can define sections and then update them with

您可以定义部分,然后使用更新它们

#1


9  

The strategy that I use is to have a base view model from which all of my view models derive. I use a base controller, though you could also use a global filter, and override OnActionExecuted. When I detect an action that returns a ViewResult, I cast the model to my base view model and set the common properties on the model from the base controller.

我使用的策略是拥有一个基本视图模型,我的所有视图模型都是从该模型中派生出来的。我使用基本控制器,但您也可以使用全局过滤器,并覆盖OnActionExecuted。当我检测到返回ViewResult的动作时,我将模型转换为基本视图模型,并从基本控制器设置模型的公共属性。

The choice between a global filter and a base controller depends on a variety of factors. If it really applies to all actions (that return view results) and you don't need injection to get access to some resources, then I'd probably go with the filter. If you need to have dependencies injected or you have some controllers where the data would be applied and others where it wouldn't (say the Admin controller), then I'd go the base controller route. You will need to remember to derive from the controller if you go with it.

全局过滤器和基本控制器之间的选择取决于多种因素。如果它确实适用于所有操作(返回视图结果)并且您不需要注入来访问某些资源,那么我可能会使用过滤器。如果您需要注入依赖项,或者您有一些控制器将应用数据,而其他控制器则不应用(例如Admin控制器),那么我将使用基本控制器路由。如果你使用它,你需要记住从控制器派生。

You could also do the same thing with the ViewBag if you don't want to derive from a common view model. I like having the strongly-typed model, but YMMV.

如果您不想从公共视图模型派生,也可以使用ViewBag执行相同的操作。我喜欢强类型模型,但YMMV。

#2


7  

You could use @Html.Action("ActionName", "ControllerName") in Your _layout file.

您可以在_layout文件中使用@ Html.Action(“ActionName”,“ControllerName”)。

Here is article about this: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

以下是关于此的文章:http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

#3


2  

Take a look at the login controls that are standard in a Razor project - these partial views access user data - probably exactly like you'd like to.

看看Razor项目中标准的登录控件 - 这些部分视图访问用户数据 - 可能与您想要的完全一样。

e.g. a typical LogonPartial.cshtml might contain:

例如典型的LogonPartial.cshtml可能包含:

@if(Request.IsAuthenticated) {
    <text>Welcome <b>@Context.User.Identity.Name</b>!
    [ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
}
else {
    @:[ @Html.ActionLink("Log On", "LogOn", "Account") ]
}

#4


1  

I believe the core to your concerns would best be served by using Sections.

我相信使用Sections可以最好地满足您关注的核心。

http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

You can define sections and then update them with

您可以定义部分,然后使用更新它们