本文介绍了ASP.NET MVC 3 _Layout.cshtml控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能帮我这个问题?我使用的Razor视图引擎,我需要一些数据传递给_layout。我该怎么办呢?
Can anyone help me with the subject? I'm using Razor view engine and I need to pass some data to _Layout. How can I do it?
推荐答案
像往常一样通过创建一个视图模型重新presenting数据开始:
As usual you start by creating a view model representing the data:
public class MyViewModel
{
public string SomeData { get; set; }
}
然后控制器将从某处读取数据:
then a controller which will fetch the data from somewhere:
public class MyDataController: Controller
{
public ActionResult Index()
{
var model = new MyViewModel
{
SomeData = "some data"
};
return PartialView(model);
}
}
然后是相应的视图(〜/查看/迈德特/ Index.cshtml
)重新present数据:
then a corresponding view (~/Views/MyData/Index.cshtml
) to represent the data:
@{
Layout = null;
}
<h2>@Model.SomeData</h2>
终于里面你的 _Layout.cshtml
某处有这样的数据:
@Html.Action("index", "mydata")
这篇关于ASP.NET MVC 3 _Layout.cshtml控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!