重写 ASP MVC EditorFor 模板并包含原始 EditorFor 参数?
我目前正在使用自定义助手重写 EditorFor 助手,如果 DateTime 对象设置了最小值,则该助手将 DateTime 对象显示为空字符串。它使用 TextBoxFor helper 来完成此操作:
<%: 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 ="编辑日期字段"。
那么,如何保留对 EditorFor 的原始调用中包含的属性?如果我将原始 id 和 class 属性添加到模板中,那么使用 DateTime 对象对 EditorFor 的所有调用都将获得特定的元素/值对,这不是我想要的。
谢谢!
I'm currently overriding the EditorFor helper with a custom one that displays a DateTime object as an empty string if the DateTime object has a min-value set. It does this using TextBoxFor helper:
<%: Html.TextBox("", (Model != DateTime.MinValue ? Model.Date.ToString("ddMMMyy") : string.Empty), new { @class = "edit-date-field" })%>
The problem is that when I use this like this:
<%: Html.EditorFor(m => m.StartDate, new { id = "field-id", @class = "field-class"})%>
i expected MVC to preserve the field-id and field-class attributes, but instead the generated element has id="StartDate" and only one class, class="edit-date-field".
So, how do I preserve the attributes that are included in the original call to EditorFor? If I add the original id and class attributes into the template, than ALL calls to EditorFor with a DateTime object will get that specific elements/value pair which is not what I want.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我澄清一下,您正在尝试使用 TextBox 来替换 EditorFor。
您原来的 TextBox html 帮助程序没有设置 id,并且 class 属性设置为 edit-date-field
试试这个:
Let me clarify, you are trying to use TextBox to replace the EditorFor.
Your original TextBox html helper does not have id set and the class attribute was set to edit-date-field
Try this: