开发者

Using DisplayFor inside a display template

I've created a htmlhelper extension to reduce the amount of repetitive markup when creating forms:

public static MvcHtmlString RenderField<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression)
{
    return htmlHelper.DisplayFor(expression, "formfield");
}

The idea being that inside my views I can just write @Html.RenderField(x=>x.MyFieldName) and it will print the label and the field's content with the appropriate div tags in place already.

Inside the displaytemplates folder I have created formfield.cshtml containing the following:

<开发者_运维技巧;div class="display-group">
<div class="display-label">
@Html.LabelFor(x => x)
</div>
<div class="display-field">
@Html.DisplayFor(x => x)
</div>
</div>

Unfortunately it doesn't appear that it is possible to nest DisplayFor inside a display template (it doesn't render anything). I don't want to just using @Model because then I won't get checkboxes for boolean values, calendar controls for dates etc.

Is there a good way around this?


You can't nest DisplayFor(). See here: Is it possible to use DisplayFor() from within the EditorFor template control

Have you considered Html.DisplayForModel()? Explanation and sample code can be found here: http://msdn.microsoft.com/en-us/library/ee430907(v=VS.100).aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜