Book Image

ASP.NET Core 2 Fundamentals

By : Onur Gumus, Mugilan T. S. Ragupathi
Book Image

ASP.NET Core 2 Fundamentals

By: Onur Gumus, Mugilan T. S. Ragupathi

Overview of this book

The book sets the stage with an introduction to web applications and helps you build an understanding of the tried-and-true MVC architecture. You learn all about views, from what is the Razor view engine to tagging helpers. You gain insight into what models are, how to bind them, and how to migrate database using the correct model. As you get comfortable with the world of ASP.NET, you learn about validation and routing. You also learn the advanced concepts, such as designing Rest Buy (a RESTful shopping cart application), creating entities for it, and creating EF context and migrations. By the time you are done reading the book, you will be able to optimally use ASP.NET to develop, unit test, and deploy applications like a pro.
Table of Contents (14 chapters)

Server-Side Validation


Let's continue with the application that we built in the previous chapter. To perform a server-side validation, we need to do the following:

  1. Add Data Annotation attributes to the ViewModels model class. The input data is validated against this metadata and the model state is updated  automatically.
  2. Update the view method to display the validation message for each of the fields. The span tag helper with the asp-validation-for attribute will be used to display the validation error message.
  3. Update the controller action method to verify the model state. If the model state is valid, we insert the data into the database. Otherwise, the ViewModel is updated and the view method is rendered again with the validation error message so that the user can update with valid input data and submit the form again.

Updating ViewModels with the Data Annotation Attribute

The Data Annotation attribute defines the validation rules for the properties of the Model/ViewModel. If the input data does...