Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've successfully created a editor template for the @HtmlEditorFor() method and the code looks like this:
C#
<div class="form-group">
    @Html.LabelFor(model => model.Birthdate, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Birthdate, "DateTime", new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Birthdate, "", new { @class = "text-danger" })
    </div>
</div>

And the partial view DateTime looks like this:
C#
@model DateTime?
@if (Model.HasValue)
{
    @Model.Value.ToString("MM/dd/yyyy")
}

... and this is located in the Shared/EditorTemplates subdirectory.

The template actually does the formatting BUT the control is no longer an editable field ... it is simply displayed as if it was simple HTML.

Any ideas as to why this is no longer displayed as and editing region for my Birthdate property?

What I have tried:

What I have tried is exactly as stated above ...
Posted
Updated 22-Mar-19 6:34am

1 solution

Because that's exactly what you've told it to do. :)

What you've got there would work fine as a display template. For an editor template, you'll need to render an editable control.

Here's a simplistic example:
Razor
@model DateTime?
@{
    var htmlAttributes = new { type = "date", @class = "form-control", placeholder = ViewData.ModelMetadata.Watermark };
}
@Html.TextBox("", Model?.ToString("yyyy-MM-dd"), htmlAttributes)

NB: The HTML date input[^] uses the unambiguous format "yyyy-MM-dd" both for the value attribute and when posting the value back to the server. Most browsers will automatically adjust this to match the user's display settings.
 
Share this answer
 
Comments
Charles T. Blankenship 22-Mar-19 12:51pm    
You ROCK Richard ... thank you soooo much!!!!
Charles T. Blankenship 22-Mar-19 15:11pm    
Once again ... that is some awesome references ... I wish I'd known this before wasting hours how to format a simple DataTime in MVC ... you have my gratitude!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900