Thursday, September 25, 2014

Use ModelState Property within the View of ASP.NET MVC 4

Hi Folks,

 

Dotnetpiper

Some time you may encounter a situation to verify your Model is valid or not .

If you want to access the ModelState property in the View , you can use the ModelState in your Razor View as depicted below in image:

Generally this property used to identify that model is valid or not.

 

This is the source code as shown below: 

  1: @model IEnumerable<MVCSample.Models.EmpRegistration>  
  2: @{  
  3:     ViewBag.Title = "Verify";  
  4: 
  5: if (@ViewContext.ViewData.ModelState.IsValid)  
  6:     {  
  7:         ViewBag.Title = "Verify";  
  8: 
  9: if (TempData["EmployeeRegistration"] != null)  
 10:         {  
 11:             var tempDataEmployeeRegistration = TempData["EmployeeRegistration"];  
 12:         }  
 13:     }  
 14: }  
 15: 
Thanks 

To learn more about MVC please go to the following link.

MVC Articles

Thanks.
Enjoy coding and reading.

0 comments :

Post a Comment