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?
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.