Creating ASP.NET MVC 5 Application using Entity Framework 6 and Identity with CRUD Operations on visual studio 2015 Part-8

This is the part-8 of asp.net mvc 5 series using entity framework and identity from scratch.In this tutorial you will learn how to make a server side validation using asp.net mvc data-annotation? How to make a partial class of  entity model class? So there is a complete step by step process of making patient registration form, if you have any query regarding this tutorial, please comment right below and your query will be answered as soon as possible. Other part of series: 

  1. Creating ASP.NET MVC 5 project Part-1?
  2. Creating SQL Server database Part-2?
  3. Creating Entity Framework project Part-3 ?
  4. Customize the default template of ASP.NET MVC 5 Part-4 ?
  5. How to add a controller and view Part-5 ?
  6. How to Add Model Part-6 ?
  7. How to make a patient registration form Part-7?

  • MVC Data Annotations for Model Validation
    • In the development of web application data validation is a key aspect.We can easily apply validation to web application by using  data annotations attribute classes to model in asp.net mvc 5.Data Annotation attribute classes are present in System.ComponentModel.DataAnnotations namespace and are availlable to Asp.net projects like Asp.net web application & website, Asp.net MVC, Web forms and also to Entity framework ORM models.Data Annotations help us to define the rules to the model classes or properties for data validation and displaying suitable messages to end users.
    • Data Annotations validator attribute:
      • DataTypeSpecify the data type of a property.
      • DisplayNameSpecify the display name for a property.
      • DisplayFormat: Specify the display format for a property like different format for date property.
      • Required:Specify a property as required.
      • RegularExpressionValidate the value of property by specified regular expression pattern.
      • RangeValidate the value of a property with in specified value of range.
      • StringLength: Specify min and max length for string property.
      • MaxLengthSpecify the max length for the string property.
      • Bind:SpecifySpecify fields to include or exclude when adding parameter or form values to model properties. 
      • ScaffoldColumnSpecify fields for hiding form editor forms.
  • MVC Data Annotations for Model Validation
    • We have created a patient form in part-7, just  run the application and see the output of patient form in browser. you will see the following output:
    • Now if you hit the save button you will see the following error page.
    • To remove this error just add link of the jquery.validate.js, jquery.validate.unobtrusive.js and jquery.unobtrusive-ajax.js files from script folder just like that:
    • Now create a Validation folder in HIMS.Model project and create a validation class Make a tblPatient partial class in HIMS.Model  and define the label display name as well as required filed attribute by using DataAnnotations class.Let's create a validation class, right on the validation folder in HIMS.Model project and go to Add > Class, enter the name of class Validation and finally click Ok. Your validation class is created and replace the whole code with the following code.
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.ComponentModel.DataAnnotations;
      namespace HIMS.Model
      {
          [MetadataType(typeof(tblPatient.Patient))]
          public partial class tblPatient
          {
              sealed class Patient
              {   
                  public string MRN { get; set; }

                  [Required]
                  [Display(Name ="First Name:")]
                  public string FName { get; set; }
                  [Display(Name ="Middle Name:")]
                  public string MName { get; set; }
                  [Required]
                  [Display(Name ="Last Name:")]
                  public string LName { get; set; }
                  public string Gender { get; set; }
                  [Display(Name ="Date of birth:")]
                  public Nullable<System.DateTime> DOB { get; set; }
                  [Display(Name ="Marital Status:")]
                  public string Marital_Status { get; set; }
                  [Display(Name ="Select country:")]
                  public Nullable<int> CountryId { get; set; }
                  [Display(Name ="Select province"")]
                  public Nullable<int> ProvinceId { get; set; }
                  [Display(Name ="Select district:")]
                  public Nullable<int> DistrictId { get; set; }
                  [Display(Name ="Select city:")]
                  public Nullable<int> CityId { get; set; }
                  [Display(Name ="Primary address:")]
                  public string PrimaryAddress { get; set; }
                  [Display(Name ="Secondary address:")]
                  public string SecondaryAddress { get; set; }
                  [Display(Name ="Mobile #:")]
                  public string MobilePhone { get; set; }
                  [Display(Name ="Work phone #:")]
                  public string WorkPhone { get; set; }
              }
          }
      }
    • Your validation class just looks like this.
    • Finally run the application and in the patient form hit the save button you can see all data annotations is applied.You can apply all the data annotations attributes and you can change the error massage as you want.
  • SummaryIn this article I try to expose the Data Annotations with example. I hope you will understand. I would like to have feedback from my blog readers. Please post your  comments about this article.

xt »

No comments:

Post a Comment

Creating ASP.NET MVC 5 Application using Entity Framework 6 and Identity with CRUD Operations on visual studio 2015 Part-9

This is the part-9 of asp.net mvc 5 series using entity framework and identity from scratch.In this tutorial you will learn how to bind a...