Server-side validation
Let us continue with the application that we built in the previous chapter. To do server-side validation, we need to do the following:
- Add Data Annotation attributes to the
ViewModels
model class. The input data is validated against this metadata and the model state is updated automatically. - Update the
view
method to display the validation message for each of the fields. Thespan
tag helper with theasp-validation-for
attribute will be used to display the validation error message. - 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 View model is updated and the
view
method is rendered again with the validation error message so that ...
Get ASP.NET Core: Cloud-ready, Enterprise Web Application Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.