DataAnnotations in Asp.net Mvc
When you are working in asp.net Model View Controller Architecture how you validate Input Controls. What are the Different types of Controls that exists in Asp.net Mvc. I want to describe here in this Article.
When You are working in asp.net Model View Controller Architecture how you validate Input Controls. What are the Different types of validation Controls that exists in Asp.net Mvc. How they use it ? I want to describe here in this Article.
In MVC MODEL DATA IS VALIDATED using a Namespace Called
using System.ComponentModel.DataAnnotations;
There are several DataAnnotations are available in Mvc which are Mainly used are Listed below
1)Required
2)RegularExpression
3) String Length Validator
4)Range Validator
Unlike Normal Asp.net Mvc Architecture follow different way of approaching using them even though the Validators name are seems to be the same but the Usage of these Validators are different. So how to Use them we will see here in the below.
Here Comes to Model Class
Suppose if a input Field is a Required and it should be string Length 10 than you have to follow the below type
[Required]
[StringLength(50)]
public string Name { get; set; }
[Required(ErrorMessage = "Pleasee enter your number")]
[Range(0, 10000, ErrorMessage = "Enter number between 0 to 10000")]
public int NumberofSales { get; set; }
if you want a Regular Expression to be implemented in Asp.net Mvc
[DisplayName("Price")]
[Required]
[RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]
public decimal UnitPrice { get; set; }
In the Controller Method
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreatePerson(Person person)
{
if (!ModelState.IsValid)
{
return View("CreatePerson", person);
}
people.Add(person);
return RedirectToAction("Index");
}
}
and in the View Page ...
<%= Html.ValidationSummary("Validation Failed for the Input Controls. Please correct the errors and try again.") %>
Use Validation Message
<%= Html.ValidationMessage("Name", "*") %>
Now see the Full Code of all the three at One time
Model Sales Class
public class Sales
{
public int Id { get; set; }
[Required]
[StringLength(10)]
public string Name { get; set; }
[Required]
public string Description { get; set; }
[DisplayName("Price")]
[Required]
[RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]
public decimal UnitPrice { get; set; }
}
}
Sales Controller Classs
sing System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class SalesController : Controller
{
//
// GET: /Sales/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Sales/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create()
{
if (!ModelState.IsValid)
return View();
// TODO: Add insert logic here
return RedirectToAction("Index");
}
}
}
SalesView Class
<%@ Page Title="" Language="C#" MasterPageFile="Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.Product>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create</h2>
<%= Html.ValidationSummary("Validation against input fields was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Name">Name:</label>
<%= Html.TextBox("Name") %>
<%= Html.ValidationMessage("Name", "*") %>
</p>
<p>
<label for="Description">Description:</label>
<%= Html.TextBox("Description") %>
<%= Html.ValidationMessage("Description", "*") %>
</p>
<p>
<label for="UnitPrice">Price:</label>
<%= Html.TextBox("UnitPrice") %>
<%= Html.ValidationMessage("UnitPrice", "*") %>
</p>
<p>
<input type="submit" value="CreateSales" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>