Friday, 9 January 2015

Validation in ASP.NET MVC 4 Razor with the Data Annotation Validators in “MODEL” in C#.NET

Hi friends,in this article I will explain about Validation in MVC4 Razor with the Data Annotation Validators in “MODEL”.
I already explained in the previous articles about Bind, Save, Edit, Update, Cancel, Delete, Paging example in GridView in ASP.NET using VB.NET,How to insert multiple rows into the GridView without using any database in ASP.NET using VB.NET/C# || how to save multiple rows in GridView into a Session.

Create MVC4 project and Add Model name it as UserDetails.
Models-->Add--->Class-->UserDetails.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
namespace MVCTest.Models
{
    public class UserDetails
    {
        [Required ]
        [Display(Name="First Name")]
        [Required(ErrorMessage="First Name Is Required")]
        public string FirstName { get; set; }
        [Required ]
        [Display(Name="Last Name")]
        [Required(ErrorMessage ="Last Name Is Required")]
        public string LastName{get;set;}
        [Required]
        [Display(Name="Address")]
        [StringLength(50)]
        [Required (ErrorMessage="Address required")]
        public string Address { get; set; }
        [Required]
        [Display(Name="DOB")]
        [DataType (DataType .Date)]
        public DateTime DOB { get; set; }
        [Range (100,1000000)]
        public decimal Salary { get; set; }

    }
}


and take one View name it as UserDetails.cshtml

@model MVCTest.Models.UserDetails

@{
    ViewBag.Title = "UserDetails";
}

<h2>UserDetails</h2>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval");
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(false)
<table>

<tr> <td>
Your Name: @Html.TextBoxFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</td> </tr>
<tr> <td>
Your Age: @Html.TextBoxFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</td></tr>
<tr> <td>
Your Email: @Html.TextBoxFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</td></tr>
<tr><td>
Your Phone: @Html.TextBoxFor(model => model.DOB)
@Html.ValidationMessageFor(model => model.DOB)
</td></tr>
<tr><td>
Your Address: @Html.TextBoxFor(model => model.Salary)
@Html.ValidationMessageFor(model => model.Salary)
</td></tr>
<tr><td>
<input type="Submit" value="Register Me" />
</td></tr>
</table>

}


and Run the application.The output of the above will like as shown in the figure.



"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook page Aspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog for more articles."

1 comment:

  1. Hi,A designer can create Web pages without having to learn actual html coding for Web Design Cochin. Microsoft FrontPage and Macromedia DreamWeaver are two of the most popular and powerful HTML editors.Thanks.....

    ReplyDelete

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.