Why is Html.TextBox a Date Validated Rendering Field

I am extracting a DateTime field from my view model into two separate form fields (date and time). I have this line of code, in my opinion:

@Html.TextBox("EndTime.Date", Model.EndTime.ToShortDateString()) 

which is displayed as:

 <input data-val="true" data-val-date="The field Date must be a date." data-val-required="The Date field is required." id="StartTime_Date" name="StartTime.Date" type="text" value="29/09/2012"> 
  • Why does this input field contain data- * attributes added to it?
  • Why does he even think it's a date?

An interesting side effect of this unexpected check is that it checks the field as a date (this is what I want to match), but jquery decides to use the wrong date format so I get an error:

"Date field must be a date."


UPDATE: It turns out that the "incorrect date format" error was caused by an error in Chrome. Since the JQuery Validation library checks the date by creating a new Date() , and since Chrome always seems to parse dates as en-US (completely ignoring the language / language settings), it failed when I entered the en-UK dates.
+4
source share
3 answers

Why does this input field contain data- * attributes added to it?

Because you have enabled unobtrusive verification in your application. By default, the MVC verification system allows you to check on the client side both the required and the data type for value types (integer, datetime).

Why does he even think it's a date?

When you pass a string to the helper method Html.TextBox , it checks to see if Model contains any property with that name, and in your case, Model has a property called EndTime and is of type DateTime .

You pass an EndTime.Date , which still represents the DateTime type ( DateTime has a Date property that is of the DateTime type, contains only the date component), and therefore two checks are allowed by MVC.

I would suggest you create two different properties for storing date and time.

+1
source

Sometimes you can get unwanted validation fields, even if you have not explicitly added a validation attribute to this property.

You can disable this by adding this to your Application_Start ():

 DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; 

Also, what might solve your problem is that your Model.EndTime property should allow null:

 DateTime? EndTime; 
+3
source

I had similar problems when you use form names that are the same as the Model object ... the form automatically associates it with the model.

If you plan to process the date / time values โ€‹โ€‹yourself after submitting the form, you can create fields on the model

 private string _endDate; public string EndDate { get { if (_endDate == null) _endDate = EndTime.ToShortDateString(); return _endDate; } set { _endDate = value; } } 

And then use EndDate as the field of your text field, or even use TextBoxFor and map it to your model. You can process the data subsequently in a message or in an installed accessory.

+1
source

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


All Articles