Mvc icon indicating copy to clipboard operation
Mvc copied to clipboard

If user submit price value (decimal / int) empty from view, server side model validation show wrong message.

Open mukundabrt opened this issue 10 years ago • 6 comments

There is issue found on model validation server side.

public string Genre { get; set; } public decimal Price { get; set; }

If user submit price value empty from view, server side model validation show wrong message ie. the value "" is not valid.

I have checked in controller, the incoming model is showing price value 0 as default but ModelState getting rawValue="". so conflicting with datatype and throw msg. Please resolve the above issue.

mukundabrt avatar Aug 16 '16 19:08 mukundabrt

The SimpleTypeModelBinder detects that string.Empty cannot be converted to a decimal value and adds the error you see. What is the issue?

dougbu avatar Aug 16 '16 19:08 dougbu

ok SimpleTypeModeBinder is detecting string.empty but model is carring 0 as default (even user submit with empty) so should validate with 0 not with "" (string.empty).

front javascript validation working fine but server side model validation show wrong error message as "the value "" is not valid"

decimal / int is alwasys getting 0 as default value.

Thanks for prompt reply

mukundabrt avatar Aug 16 '16 20:08 mukundabrt

We're talking about a model binding error -- some invalid data on the wire gets caught up front. Validation and default values for properties that aren't assigned aren't relevant here.

dougbu avatar Aug 16 '16 21:08 dougbu

In MVC5 its working fine but in MVC6 its showing error like above mentioned.

mukundabrt avatar Aug 17 '16 17:08 mukundabrt

Either MVC 5 did not show the same error in this situation or it didn't catch the error. Either way, the MVC 6 behaviour is correct -- the submitted value "" is not valid for a decimal property.

More generally why do you prefer a different behaviour? What is your overall scenario that requires this problem not be caught?

For example, if you prefer the model binding problem is ignored, clear the errors for that property in the ModelStateDictionary.

dougbu avatar Aug 17 '16 19:08 dougbu

Thank u very much. Problem solved I have identified the cause of problem.

mukundabrt avatar Aug 20 '16 09:08 mukundabrt