MVC .Net Core Model Validation - The value '' is invalid. Error

╄→гoц情女王★ 提交于 2019-11-29 05:39:55

In order to make your Required attribute works you need to make field nullable:

public DateTime? AppointmentDate { get; set; }

Edit: also note that DataType attribute actually doesn't perform validation on field. MVC validate date when applying binding from post data to model

Having the same problem but cannot detect the problem. I checked the object in debug mode to see if is there any way to see which property fails the model state.

Then I see the which one fails model. That is a boolean value which maps to a checkbox

Weird part is "this is not a Required field"!

I added a question mark and used getvalueordefault method when using it

public bool? IsCorporateAccount { get; set; }

My issue was that in the Create Action, I'd return the View without newing up an Entity, which, when I changed that, i.e. Return View(new MyEntity()); it fixed the issue

My specific problem had to do with select option validation showing a similar message instead of my custom message from the model. In addition to the answer about making the validated property nullable, double check the actual value in the DOM element. My problem was solved by adding nullability and an empty string as the value in the default option which is supposed to be an int.

<select asp-for="@Model.Thing" asp-items="Model.Things"
    <option value="" selected>Please select a Thing</option>
</select>
<span asp-validation-for="@Model.Thing" class="text-danger"></span>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!