Html.TextBoxFor formatting or Html.EditorFor htmlAttributes?

48,327

Solution 1

I ended up solving this by creating a custom editor template for my date picker as so:

Shared/EditorTemplates/DateTime.cshtml

 @model System.DateTime? 
 @Html.TextBox("", Model.HasValue ? Model.Value.ToString("dd/MM/yyyy") : string.Empty, new { @class = "date-picker" })

Then in my original page continue to use

@Html.EditorFor(m => m.DateModified)

Solution 2

You could...

@Html.TextBoxFor(m => m.DateModified, new { Value = Model.DateModified.ToString("MM-dd-yyyy"), @class = "superCoolClassName"})

Solution 3

Use @Html.EditorFor(m => m.DateModified), because otherwise the DisplayFormat attribute will have no effect.

To add further attributes like a CSS class, you have to create an editor template for the DateTime. Create a file EditorTemplates/DateTime.cshtml with the following content:

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new
{
    @class="date"
})

Please note that the value of the TextBox is not set with the Model directly, but rather with the TemplateInfo.FormattedModelValue, because that value will be formatted according to the DisplayFormat attribute while the Model not. (This took me quite some time to realize. :))

In simple cases this might be enough, e.g. if the CSS class can be the same for all date editors.

If you want to parametrize the attribute, you can do that as well, passing the attribute value parameter to the EditorFor.

@Html.EditorFor(m => m.DateModified, new { @class = "someClass" })

However, this parameter will be not automagically delegated to the HTML control as attribute, but you have to "handle it" in the template explicitly. According to my experiences you can access this parameter value in the ViewData in the template, so the parametrized template looks like this:

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new
    {
        @class=ViewData["class"]
    })

Solution 4

To prevent hardcoding the key/value pairs listed in EditorFor , convert the ViewData object to a Dictionary and pass that dictionary object to TextBox.

eg

    @Html.EditorFor(m => m.DateModified, "Template", new { @class = "someClass", size=8 , htmlTag="custom" }) 

And in the template you have

    @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, ViewData.ToDictionary(c=>c.Key,c=>.Value))
Share:
48,327
lahsrah
Author by

lahsrah

Updated on July 15, 2020

Comments

  • lahsrah
    lahsrah almost 4 years

    I am kind of stumped because, I want to format the value and add a html attribute for css class.

    If I use @Html.TextBoxFor(m => m.DateModified) - I can add html attribute but formatting does not work via DisplayFormat attribute on the member.

    If I use @Html.EditorFor(m => m.DateModified) - Formatting works but I cannot add html attribute

    If I use @Html.TextBox("DateModified", Model.DateModified, ...) - I get null reference exception when Model is null when the form is in add mode

    What is the best way to achieve this?