加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在显示模板中使用DisplayFor

发布时间:2020-12-16 09:45:27 所属栏目:asp.Net 来源:网络整理
导读:我创建了一个htmlhelper扩展,以减少创建表单时重复标记的数量: public static MvcHtmlString RenderFieldTModel,TValue(this HtmlHelperTModel htmlHelper,ExpressionFuncTModel,TValue expression){ return htmlHelper.DisplayFor(expression,"formfield")
我创建了一个htmlhelper扩展,以减少创建表单时重复标记的数量:

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

想法是在我的观点中我可以写@ Html.RenderField(x => x.MyFieldName),它将打印标签和字段的内容,并且已经有适当的div标签.

在displaytemplates文件夹中,我创建了包含以下内容的formfield.cshtml:

<div class="display-group">
<div class="display-label">
@Html.LabelFor(x => x)
</div>
<div class="display-field">
@Html.DisplayFor(x => x)
</div>
</div>

不幸的是,似乎没有可能在显示模板中嵌套DisplayFor(它不会渲染任何东西).我不想只使用@Model,因为那样我就不会得到布尔值的复选框,日期的日历控件等.

这有什么好办法吗?

解决方法

你不能嵌套DisplayFor().见: Is it possible to use DisplayFor() from within the EditorFor template control

你考虑过Html.DisplayForModel()吗?可在此处找到说明和示例代码:http://msdn.microsoft.com/en-us/library/ee430907(v=VS.100).aspx

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读