我需要用特定的名稱,添加的東西一節這樣的:添加具有特定名稱的部分,我的索引
@string test="test";
@section @test
{
@* my view code *@
}
我怎麼能得到這個工作。
我需要用特定的名稱,添加的東西一節這樣的:添加具有特定名稱的部分,我的索引
@string test="test";
@section @test
{
@* my view code *@
}
我怎麼能得到這個工作。
您可以使用DefineSection
方法。在這裏看看這個如何有用的例子:http://blogs.msdn.com/b/marcinon/archive/2010/12/15/razor-nested-layouts-and-redefined-sections.aspx
你可以使用一個部分可能使用Html.Partial
助手在任何地方包括:
@Html.Partial("test")
這將使其中可能包含你喜歡的任何標記的Test.cshtml
局部視圖。
如果您想要處理一些更復雜的場景,其中此部分將包含來自某些數據存儲的動態數據,則可以使用Html.Action幫助程序。
public class MyModel
{
public string SomeProperty { get; set; }
}
然後控制器:你會通過定義模型開始
public class MySectionController: Controller
{
public ActionResult Index()
{
MyModel model = ...
return View(model);
}
}
,並有相應的視圖:
@model AppName.Models.MyModel
<div>@Model.SomeProperty</div>
最後你會包括像這樣:
@Html.Action("index", "mysection")
You co將它包含在你的佈局中,這樣你就可以將動態組件與你的主控制器邏輯完全分離,並且每個組件都遵循自然的工作流程。
你在哪裏試圖添加節?並且您想要對該部分做些什麼 - 這是一種偏視還是一種觀點? – stack72 2011-01-27 14:00:34