How to override the value of "A". when checking datetime format in asp.net mvc view?

I check the datetime field in the mvc view for the datetime format as follows:

If contactToValidate.LastUpdated = Nothing OrElse Not IsDate(contactToValidate.LastUpdated) OrElse CType(contactToValidate.LastUpdated, DateTime) = DateTime.MinValue Then _validationDictionary.AddError("AddErrorValue", "Date Format is not Correct!") End If 

but if the input for LastUpdated in the view, left blank, next to my own error ("Date format is not correct!"), a default error message is displayed: a value is required. I believe that this is what the mvc framework automatically checks for data type conversions, but since I check the LastUpdated text field for null and datetime format, I do not want this error message to be displayed. I just want mine. How can i do this? Thanks

+4
source share
7 answers

You are probably getting this message because the date field is bound to an undefined DateTime in your presentation model. If you change the type of this field or property to a DateTime value of zero, you will no longer receive this message.

In any case, you can customize the message that the user sees by inserting IDataErrorInfo into your presentation model.

+4
source

Unfortunately, no matter where you look, most of the answers to this question are: "Deploy IDataErrorInfo!".

The same problem arises if, for example, you try to enter a text string in a text field field bound to the integer property. Simply, if MVC cannot convert the user value to the appropriate type, it generates these (completely useless) common errors and never calls IDataErrorInfo members.

It is definitely a hassle to redefine these useless messages, and personally, I find the best answer is simply to implement some client-side validation to handle these scenarios. The cases when it generates these messages are very consistent and easily predictable:

  • The user did not provide a value for the field with a null value.
  • The user entered or selected a value that cannot be converted to a base type (for exmaple: text in a numeric field)

In addition, client-side validation is simply good practice. If the user does not have javascript enabled, they will of course be able to figure out the problem if you use Html.ValidationMessage () and / or style the input fields to identify the problems.

+4
source

I tried to inject IDataErrorInfo into my partial classes, but I still got the error message "Value is required" for dropdownlists, for which nothing was selected in it, i.e. The return value is null, not an integer for the foreign key.

I did this in the first line of my Create action:

 public ActionResult Create([Bind(Exclude="Id")]MyEntity myEntityToCreate) { foreach (string key in ModelState.Keys) { ModelState[key].Errors.Clear(); } 

and then rely on my model authentication code, which runs when the object is saved, to add meaningful error messages to the ModelState collection when the dropdowns are not selected.

This is probably not the best way to do this.

+3
source

I also ran into this problem - this really happens when the model field is bound to a type with a null value in your data model.

The way I got around this is to clear the error information in ModelState. In my exception handler in my controller method, I do this:

 foreach (var state in ModelState.Values) { if (state.Errors.Count > 0) state.Errors.Clear(); } 

which removes the default validation behavior and then runs my own. Please note that doing this will lead to errors, but not a description unless your own validation covers all of your fields. If this is only one field that you want to clear, you can check its name in the ModelState dictionary and just clear it.

+3
source

If you want to exclude a property from verification, then the best way to do this is if you use model binding, then you want:

 public ActionResult Create([Bind(Exclude="LastUpdated")] Contact contactToValidate) { } 

if, on the other hand, you are using UpdateModel or TryUpdateModel, you can use:

 string prefix = string.Empty; string[] includedProperties = new string[0]; string[] excludedProperties = new [] { "LastUpdated" }; TryUpdateModel<Contact>(contactToValidate, prefix, includedProperties, excludedProperties); 

Note that since you are sending an empty array for the included properties, you actually include ALL properties, and one excluded property is then taken from this set.

+1
source

I believe what you are looking for is

 ViewData.ModelState.AddModelError("FieldName", "Date Format is not Correct!"); 

This will make <%=Html.ValidationMessage("FieldName") %> show your error message.

Edit: I just checked and this will not solve the problem. You should contact Craig Stuntz to answer the question about non-null type and IDataErrorInfo , and then you can add your own error message for an empty field.

0
source

I know this is a little late. But the real problem is having a property named "Id" in the binding model. Just rename all the "Id" fields that you have on EntityId or something like that. And MVC will not auto-confirm the field.

0
source

All Articles