Introduction

In my last article series I explained some basic operation like Create, List, Edit, Detail and Delete using Scaffolding.

In my last article series I didn’t explain any validation on data. As we entered the blank form it will simply entered the data.

For Example

Perform create option but do not fill any data in controls and submit the form and then check your list

		
		validation in MVC
		Figure 1
		
		

If you click on create button and after that check the list you will find a blank row will be added.

		
		server side validation

		Figure 2
		
		

As we don’t want to save blank row in database, it must show error if user try to upload blank data. For this we have to first use TryUpdateModel which will tell that whether the user entered the data or not. And using IsValid property we can check it.

Note: - I already explain this property in my previous article series

To implement this changes we have to make the following changes in Create action which I have created in my previous article i.e. Scaffolding Create option

Home Controller Code

		
		[HttpGet]
        [ActionName("Create")]
        public ActionResult Create_Get()
        {

            return View();
        
        }

        [HttpPost]
        [ActionName("Create")]
        public ActionResult Create_Post()
        {
            MvcApplication1.Models.Student stu=new Models.Student();
            TryUpdateModel(stu);
            if (ModelState.IsValid)
            {
                //this method is used to insert data in database using LINQ
                dc.GetTable<MvcApplication1.Models.Student>().InsertOnSubmit(stu);
                dc.SubmitChanges();
                return RedirectToAction("Index");
            }
            else
            {

                return View();

            }

        }

	
		

As you can see the code, if tryupdatemodel fills the data successfully then IsValid property will return true otherwise it will return false. So again execute the code and this time when you submit the empty form you will get following screen:-

		
		Required Validation in MVC

		Figure 3
	
		

As you can see the it’s showing validation error for age and fees but not for name. first of all I want to explain you how its showing error. When we added create form using scaffolding it automatically added validation for each control as you can see the code of Create.cshtml.

Now I want to explain why it is not giving error for name. as we know that name is the string type so it automatically take null for name but as we know that int type cannot take null until we will not make it nullable type.

Note:-I already explain this concept also in my previous article series.

So if we want to fire proper validation, so first of all we have to go to the model class which is Student class and we have to make every column Required.

We have to add Required attribute on each column. And this attribute stored in following namespace:-

System.ComponentModel.DataAnnotations

Code of Student.cs

		
		
		using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel.DataAnnotations;


namespace MvcApplication1.Models
{
   [Table(Name="Student")]
    public class Student
    {
       [Column(IsPrimaryKey=true,IsDbGenerated=true)]
      public int stu_id { get; set; }
       [Column]
       [Required]
      public string stu_name { get; set; }
        [Column]
       [Required]
       public int stu_age { get; set; }
        [Column]
       [Required]
       public string course { get; set; }
        [Column]
       [Required]
       public int fees { get; set; }
    }
}

		
		

Now again execute this code and now again submit the empty form and you will get following window:-

		
		Required Validation in MVC

		Figure 4
	
		

And now fill the form and then again submit it

		
		
		MVC Scaffolding detail step 5
		Figure 5
		
		

After submitting the create button check the list and you will get following output:-

		
		
		MVC Scaffolding detail step 5
		Figure 6
		
		

Change Error Message

If you want to change the validation message then you can pass the named parameter i.e ErrorMessage in the following manner:-

		
		[Column]
        [Required(ErrorMessage="Enter your Name")]
      public string stu_name { get; set; }
        [Column]
        [Required(ErrorMessage="Enter your age")]
       public int stu_age { get; set; }

		
		

Now execute the code and you will get your custom error message

		
		
		MVC Scaffolding detail step 5
		Figure 7
		
		

Email Address

For any query you can send mail at info@techaltum.com
Thanks