Explain Model Validation.

 Model Validation

They will allow you to enforce various kinds of rules for your properties, which will be used in your views and in your Controllers, where you will be able to check whether a certain Model is valid in its current state or not (e.g. after a FORM submission). Let's add just a couple of basic validation to the WebUser

public class WebUser {

[Required] [StringLength (25)]

public string FirstName { get; set; }

[Required] [StringLength(50, MinLength (3)]

public string LastName (get; set; } }

[Required]

[EmailAddress] public string MailAddress { get; set; }

}

  • Notice how the three properties have all been decorated with DataAnnotations. First of all, all properties have been marked with the [Required] attribute, meaning that a value is required it can't be NULL.
  •  [StringLength] attribute makes requirements about the maximum, and in one case minimum, length of the strings. These are of course particularly relevant if your Model corresponds to a database table, where strings are often defined with a maximum length.
  • For the last property, the [EmailAddress] attribute ensures that the value provided looks like an e-mail address.

Comments

Popular posts from this blog

What are different steps used in JDBC? Write down a small program showing all steps.

Discuss classification or taxonomy of virtualization at different levels.

Pure Versus Partial EC