我在我的模型模板中广泛使用以下代码片段.
<div class="control-group"> @Html.LabelFor(model => model.FirstName) <div class="controls"> @Html.TextBoxFor(model => model.FirstName,new { @class = "span3" }) @Html.ValidationMessageFor(model => model.FirstName) </div> </div>
是否可以将其一般性地封装在编辑器模板中,这样我就可以使用Html.EditorFor(…)而不需要使用自定义扩展?
解决方法
Is it possible to encapsulate this generically in an editor template
so I can use Html.EditorFor(…) without resorting to a custom
extension?
当然:
〜/查看/共享/ EditorTemplates / Foo.cshtml:
<div class="control-group"> @Html.Label("") <div class="controls"> @Html.TextBox("",ViewData.TemplateInfo.FormattedModelValue,new { @class = "span3" }) @Html.ValidationMessage("") </div> </div>
接着:
@Html.EditorFor(x => x.FirstName,"Foo")
要么:
[UIHint("Foo")] pubilc string FirstName { get; set; }
接着:
@Html.EditorFor(x => x.FirstName)