From the course: Learning ASP.NET Core MVC

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Validate form post data

Validate form post data

From the course: Learning ASP.NET Core MVC

Start my 1-month free trial

Validate form post data

- [Instructor] Now that we've got data posting from the browser, back to to a controller action, let's validate that data so that we can make sure only clean, legitimate data makes it into our system. ASP.NET Core MVC has a really interesting way of specifying validation rules and that is by attaching them right to the properties that they are validating. Since our form is posting data via the post class, let's just start decorating that class with validation rules. To begin with, let's mark the title and body properties as required, meaning that they have to have at least some text in them in order to be valid. To do this, simply add the required attribute above each of them. This attribute lives in the System.ComponentModel.DataAnnotations namespace so be sure to import that. Next, let's specify minimum and maximum lengths for those two fields as well. Let's say, I want the Title property to be at least five characters long but no longer than 100 characters. To enforce this, I'll…

Contents