Html.HiddenFor DateTime formats incorrectly in ASP.NET

I am writing an ASP.NET MVC3 application in C # and found that calling Html.HiddenFor in my view would display DateTime differently (and incorrectly) if I were to call Html.DisplayFor .

The model in which it takes a value has a DisplayFormat decorator, and this seems to work for Html.DisplayFor . The corresponding property is written as:

 [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] public DateTime MeetingStartDate { get; set; } 

And the view displays this using:

  @Html.DisplayFor(model => model.MeetingStartDate) @Html.HiddenFor(model => model.MeetingStartDate) 

Calling DisplayFor will display the date as 16/04/2012 , however HiddenFor will display it as value="04/16/2012 00:00:00" .

I tried changing the current culture to set DateTimeFormat , but that did not affect.

The current culture is en-GB, so it should not print dates in the USA in any case.

+6
source share
5 answers

An in-view alternative using TextBoxFor will work if you specify a date format and a hidden html attribute.

 @Html.TextBoxFor(model => model.CreatedDate, "{0:dd/MM/yyyy HH:mm:ss.fff}", htmlAttributes: new { @type = "hidden" }) 
+18
source

If you want to generate a hidden field that matches the format you define, you can define a custom editor template to override the default value ( ~/Views/Shared/EditorTemplates/HiddenInput.cshtml ):

 @if (!ViewData.ModelMetadata.HideSurroundingHtml) { @ViewData.TemplateInfo.FormattedModelValue } @Html.Hidden("", ViewData.TemplateInfo.FormattedModelValue) 

and now you decorate your model property with the [HiddenInput] attribute:

 [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] [HiddenInput(DisplayValue = false)] public DateTime MeetingStartDate { get; set; } 

and in your opinion:

 @Html.DisplayFor(model => model.MeetingStartDate) @Html.EditorFor(model => model.MeetingStartDate) 

which will use the correct format for the hidden value:

 <input data-val="true" data-val-required="The MeetingStartDate field is required." id="MeetingStartDate" name="MeetingStartDate" type="hidden" value="15/03/2012" /> 
+8
source

Or you can outsmart the implementation of HiddenFor and reuse TextBoxFor instead and manually set type = "hidden".

Something like that:

 public static MvcHtmlString HiddenInputFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes = null) { IDictionary<string, object> htmlAttributesTmp = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); if (htmlAttributesTmp.ContainsKey("type")) { htmlAttributesTmp["type"] = "hidden"; } else { htmlAttributesTmp.Add("type", "hidden"); } return html.TextBoxFor(expression, htmlAttributesTmp); } 
+4
source

In your model, for the Get function of the MeetingStartDate property, can you return a formatted DateTime?

DisplayFormat will not work for hidden values ​​because MVC creates a value in a hidden field that can be correctly parsed during the form submit action from the data that is currently set for the property.

You have the expected MVC actions.

0
source

If you give id, then the problem does not occur

 <%= Html.TextBoxFor(model => Model.DiscountRate,new { @id = "DiscountRate", @Value=Model.DiscountRate})%> 
0
source

Source: https://habr.com/ru/post/910832/


All Articles