MVC data annotation for Model Validation

In this blog, we will see how data annotation work in MVC. We will also see how we can implement form validation using Data Annotation for Model.

Data Annotation

Form validation is always the primary task for a developer, either you can do this by using Client Side or Server Side.
Data Annotation is doing the same in MVC. A default feature of validating model data using attributes in MVC is known as Data Annotations.

Few Data Annotation attributes are –

  • Required
  • DisplayName
  • Range
  • StringLength
  • MaxLength
  • MinLength

All the above mentioned Data Annotation attribute comes under –  System.ComponentModel.DataAnnotations namespace.

Let’s demonstrate Data Annotation

Create a Model Class – ReviewModel

public class ReviewModel
    {
        [Required]
        [Display(Name = “Product or Service”)]
        public string ReviewFor { get; set; }
        [Required]
        [Display(Name = “Review Title”)]
        public string ReviewTitle { get; set; }
        [Required]
        [Display(Name = “Product Category”)]
        public string ProductCategory { get; set; }
        public string ProductSubCategory { get; set; }
        [Required]
        [Display(Name = “Product/Service Brand”)]
        public string ProductBrand { get; set; }
        [Required]
        [Display(Name = “Product Model”)]
        public string ProductModel { get; set; }
        [Required]
        [Display(Name = “Purchase Mode”)]
        public string PurchaseMode { get; set; }
        [Required]
        [Display(Name = “Purchase Month”)]
        public string PurchaseMonth { get; set; }
        [Required]
        [RegularExpression(@”^(d{4})$”, ErrorMessage = “Enter a valid 4 digit Year”)]
        [Display(Name = “Year”)]
        public string PurchaseYear { get; set; }
    }

Below is the View

<div class=”form-group”>
                                    @Html.Label(“Product/Service or anything”)
                                    @Html.TextBoxFor(model => model.ReviewFor, new { placeholder = “Example: iPhone 7, Dell Laptop, Samsung TV” })
                                    @Html.ValidationMessageFor(model => model.ReviewFor, “”, new { @class = “text-danger” })
                                </div>
                                <div class=”form-group”>
                                    @Html.Label(“Review Title”)
                                    @Html.TextBoxFor(model => model.ReviewTitle, new { placeholder = “Example: iPhone 7 is amazing. I love this product” })
                                    @Html.ValidationMessageFor(model => model.ReviewTitle, “”, new { @class = “text-danger” })
                                </div>
                                <div class=”form-group”>
                                    @Html.Label(“Product/Service Brand”)
                                    @Html.TextBoxFor(model => model.ProductBrand, new { placeholder = “Example: Apple” })
                                    @Html.ValidationMessageFor(model => model.ProductBrand, “”, new { @class = “text-danger” })
                                </div>

Output Screen

Don’t get confused with too many attributes or property in Model class. You may just try by adding 2 or 3 properties in Model class.

Hope you understand the use of Data Annotation in MVC.
Please share this blog on social media.

Next –

Happy Learning 🙂

Leave a Comment

RSS
YouTube
YouTube
Instagram