Override ASP MVC EditorFor template and include original EditorFor arguments?
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 Text开发者_如何学运维BoxFor 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!
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:
<%: Html.TextBox("field-id", (Model != DateTime.MinValue ? Model.Date.ToString("ddMMMyy") : string.Empty), new { @class = "field-class" })%>
精彩评论