...

/

Adding Validation to Updating a Question

Adding Validation to Updating a Question

Learn to add validations to update a question.

Steps to add validation to update a question

Let’s add validation to the request for updating a question:

  1. Open QuestionPutRequest.cs and add the following using statement:

Press + to interact
using System.ComponentModel.DataAnnotations;
  1. Add the following validation attribute to the Title property:

Press + to interact
public class QuestionPutRequest
{
[StringLength(100)]
public string Title { get; set; }
public string Content { get; set; }
}

We are making sure that a new title doesn’t exceed 100 characters.

  1. Let’s run the app and give this a try by updating a question to have a very long title:

Press + to interact
Validation error when updating a question with a long title
Validation error when updating a question with a long title

A validation error is returned as expected.

  1. Stop the app running so that we’re ready to add the next piece of validation. ...

Test yourself