mvc3, editor template, css clas, maxlength and size

瘦欲@ 提交于 2020-01-11 12:07:56

问题


I have an editor template as following but class, maxlength and size attributes are not getting to the source.

@using System.Globalization
@model DateTime?
@Html.TextBox("", (Model != null && Model.HasValue && !Model.Value.ToString(CultureInfo.InvariantCulture).Contains("1900") && !Model.Value.ToString(CultureInfo.InvariantCulture).Contains("0001") ? Model.Value.ToString("MM/dd/yyyy") : string.Empty), new { @class = "datePicker", maxlength = "12", size = "12" })

I have changed it to following and it is still the same

@Html.EditorFor(x => x.Criteria.FromDate, new { @class = "datePicker", maxlength = "12", size = "12" })

Source

<input class="text-box single-line" id="Criteria_FromDate" name="Criteria.FromDate" type="text" value="" />

How can i fix this?


回答1:


Make sure your Editor template is named DateTime - placed in folder Views/Shared/EditorTemplates, and your model (Criteria.FormDate) is same type as EditorTemplate model (DateTime?).

If all DateTime fields will have same maxlength and size you can keep them hardcoded in your EditorTemplate. Example for your html:

@EditorFor(x => x.Criteria.FormDate) //no need to pass html attributes object if they are not used in the editor template

-- its worth trying @EditorFor(model, "EditorTemplateName") to explicitly say you want that TemplateEditor for passed model. This is the case when you have multiple editors for same model type, so you call them explicitly(works like calling partial view and passing model to it).

EDIT: After looking at your template, it seems to me that your Criteria.FormDate is non nullable. You should look at improving/refactoring your code in template.



来源:https://stackoverflow.com/questions/12050109/mvc3-editor-template-css-clas-maxlength-and-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!