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.
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 (ErrorMessage="Address required")]
        public string Address { get; set; }
        [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";

@section Scripts {
@using (Html.BeginForm())

<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)
<tr> <td>
Your Email: @Html.TextBoxFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
Your Phone: @Html.TextBoxFor(model => model.DOB)
@Html.ValidationMessageFor(model => model.DOB)
Your Address: @Html.TextBoxFor(model => model.Salary)
@Html.ValidationMessageFor(model => model.Salary)
<input type="Submit" value="Register Me" />


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

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.....


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