2010-10-23 56 views

回答

3

你可以实现与Razor视图引擎同样的事情。

型号:

public class MyViewModel 
{ 
    public string Value { get; set; } 
} 

控制器:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     var model = new MyViewModel 
     { 
      Value = "foo" 
     }; 
     return View(model); 
    } 
} 

查看:

~/Views/Home/Index.cshtml

@model MyApp.Models.MyViewModel 

@{ Html.BeginForm(); } 

    @Html.EditorFor(x => x.Value) 
    <input type="submit" value="OK" /> 

@{ Html.EndForm(); } 

~/Views/Home/EditorTemplates/Template.cshtml

<p>Some text before template</p> 
@RenderBody() 
<p>Some text after template</p> 

~/Views/Home/EditorTemplates/string.cshtml

@model System.String 
@{ 
    Layout = "~/Views/Home/EditorTemplates/Template.cshtml"; 
} 
<div>@Html.TextBoxFor(x => x)</div> 

注意如何string编辑模板已定制,并作为主布局Template.cshtml