将数据从控制器传递到视图布局的最佳做法是什么? [英] What is the best practice of passing data from a controller to a view layout?

查看:261
本文介绍了将数据从控制器传递到视图布局的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前拥有一个MVC网站,需要在每个页面的标题上拥有动态内容。



我目前在控制器和地点中正常获取所需的数据它在一个视图模型。在视图中,我拿取数据并将模板部分粘贴到Viewbag中,最后,在主布局页面上,我使用Viewbag数据并将其传递给控制标题的部分。



我已经看到,我不应该在可能的情况下使用Viewbag,而且我传递数据的次数不太合适。



我可以想到改进这一点的唯一方法是在主要布局上创建一个部分,然后将部分/数据放在视图的一部分中 - 但是,有〜30页,这再一次感觉就像正确的路线。



有没有更好的方式来做这个/最好的做法是采取动态数据必须去主视图? p>

解决方案

您可以使用Child Actions执行此操作。您可以重复使用此操作,甚至将其包含在_Layout页面中。



这是一个小孩操作来显示一些标题信息。它被标记为 ChildActionOnly ,因此它只能在另一个视图中调用。它还利用 OutputCache 将结果保存5分钟。

  [ChildActionOnly] 
[OutputCache(Duration = 5 * 60)]
public ActionResult标题(字符串部分)
{
var info = new HeaderInfo
{
Section = section,
Today = DateTime.Now
};
return PartialView(_ header,info);
}

_header.cshtml

  @model HeaderInfo 
< div>
< span> @ Model.Section< / span>
< span> @ Model.Today.ToLongTimeString()< / span>
< / div>

然后在视图或布局中使用 Html.Action()或.RenderAction()

  @ Html.Action(Header,Home,new {section =Cake})
//或
@ {Html.RenderAction(Header ,Home,new {section =Pie});}

您可以指定



_Layout.cshtml

  @RenderSection(header,required:false)

主视图

  @section header {
@ {Html.RenderAction标题,首页,新{section =蛋糕})}
}


I currently have a MVC site that needs to have dynamic content on the header of every page.

I currently get the required data as normal in the controller and place it in a View Model. In the view, I take the data and stick the template parts in to the Viewbag and finally, on the main layout page, I take the Viewbag data and pass it to the partial which controls the header.

I've read that I shouldn't use Viewbag where possible, and the amount of times I pass the data round just doesn't feel right.

The only way I can think of to improve this is to create a section on the main layout, and then put the partial/data in the section of the view - however, there are ~30 pages and this again doesn't feel like the correct route.

Is there a better way to do this/what are the best practices for taking dynamic data that has to go to the main view?

解决方案

You can do this with Child Actions. You can reuse this action and even include it in the _Layout page.

Here's a child action to display some header info. It is marked ChildActionOnly so it can only be called within another view. It also takes advantage of OutputCache to save a result for 5 minutes.

[ChildActionOnly]
[OutputCache(Duration = 5 * 60)]
public ActionResult Header(string section)
{
    var info = new HeaderInfo
    {
        Section = section,
        Today = DateTime.Now
    };
    return PartialView("_header", info);
}

_header.cshtml

@model HeaderInfo
<div>
    <span>@Model.Section</span>
    <span>@Model.Today.ToLongTimeString()</span>
</div>

Then use this in a view or layout with Html.Action() or .RenderAction().

@Html.Action("Header", "Home", new { section = "Cake" })
// or
@{Html.RenderAction("Header", "Home", new { section = "Pie" });}

You can specify a section inside your layout then conditionally render if present in the view.

_Layout.cshtml

@RenderSection("header", required: false)

main view

@section header {
    @{Html.RenderAction("Header", "Home", new { section = "Cake" })}
}

这篇关于将数据从控制器传递到视图布局的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆