使用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
用作主布局。