In this tutorial, we look at how ASP.NET core Model Validation works. Often the data entered by the user is not valid and cannot be saved into the database. The entered data may contain a typo or user may intentionally enter the inappropriate data. Hence, we need to validate the user input before storing it in the database. The ASP.NET Core gives us Model Validator, which uses the validation attributes to validate the model, which makes our task easier. We also take a look at ModelState and how to use it. Finally, we look at the list of Validation attributes.
In this Model binding in ASP.NET Core article, we will learn How to pass data from View to Controller. We learn what is Model binding is and how it works. The ASP.NET core allows us to bind data from various binding sources like HTML Forms using [FromForm], Route Values using [FromRoute], Query string using [FromQuery], Request body using [FromBody] and From Request Header using [FromHeader]. We will look at all these in this chapter
The Form Tag Helper targets the HTML form element and generates the action URL & action method attributes. It uses the various server-side attributes like asp-controller, asp-action, asp-route- etc to generate the form element in the view
The label Tag Helper generates the appropriate
The Input Tag Helper generates the appropriate <input> HTML element for the model property. The model property is bound using the asp-for attribute. The input tag helper generates the appropriate HTML type, name & id attribute based on the Property data type & data annotations applied to the ViewModel. The ViewModel must be strongly typed to the View. It also emits the Validation related attributes, which helps in unobtrusive client-side validation. The asp-format attributes help in generating the properly formatted input elements.