【问题标题】:Override ASP MVC EditorFor template and include original EditorFor arguments?覆盖 ASP MVC EditorFor 模板并包含原始 EditorFor 参数?
【发布时间】:2011-02-10 13:37:15
【问题描述】:

如果 DateTime 对象设置了最小值,我目前正在使用自定义帮助器覆盖 EditorFor 帮助器,该帮助器将 DateTime 对象显示为空字符串。它使用 TextBoxFor 助手来做到这一点:

<%: Html.TextBox("", (Model != DateTime.MinValue ? Model.Date.ToString("ddMMMyy") : string.Empty), new { @class = "edit-date-field" })%>

问题是当我这样使用时:

<%: Html.EditorFor(m => m.StartDate, new { id = "field-id", @class = "field-class"})%>

我希望 MVC 保留 field-id 和 field-class 属性,但生成的元素具有 id="StartDate" 并且只有一个类,class="edit-date-field"。

那么,如何保留包含在对 EditorFor 的原始调用中的属性?如果我将原始 id 和 class 属性添加到模板中,那么使用 DateTime 对象对 EditorFor 的所有调用都将获得我不想要的特定元素/值对。

谢谢!

【问题讨论】:

    标签: asp.net asp.net-mvc templates editorfor


    【解决方案1】:

    让我澄清一下,您正在尝试使用 TextBox 来替换 EditorFor

    您原来的 TextBox html 助手没有设置 id 并且 class 属性设置为 edit-date-field

    试试这个:

    <%: Html.TextBox("field-id", (Model != DateTime.MinValue ? Model.Date.ToString("ddMMMyy") : string.Empty), new { @class = "field-class" })%>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-21
      • 2012-03-20
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多