Introduction: In
this article I will explain how we can implement the Server side validation in
MVC Razor code first approach using data annotation.
Description:
In the previous article I have explained URL routing in asp.net website, Populate Dropdown List dynamically using Asp.net MVC Razor, Code First migration in asp.net MVC 4, Populate Cascading Dropdown List in Asp.net MVC4 using Json and Jquery and What is Asp.net MVC? Its advantages and disadvantges.
Server side validations are required to receive the correct
and valid data.
Here I add a class to Model named Employee.cs. Add the namespace to class:
using System.ComponentModel.DataAnnotations;
using
System.ComponentModel.DataAnnotations.Schema;
public class Employee
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Display(Name = "Employee
Name")]
[Required(ErrorMessage = "Please
Enter Employee name")]
public string Name { get; set; }
[Display(Name = "Email
Address")]
[Required(ErrorMessage = "Please
Enter Email address")]
[RegularExpression(".+@.+\\..+",
ErrorMessage = "Please Enter Correct
Email Address")]
public string Email { get; set; }
[Display(Name = "Mobile
No.")]
[Required(ErrorMessage = "Please
Enter Mobile No")]
[StringLength(10, ErrorMessage = "The
Mobile must contains 10 characters",
MinimumLength = 10)]
public int MobileNo { get; set; }
[Display(Name = "Address")]
[Required(ErrorMessage="Please
Enter Address")]
[MaxLength(100)]
public string Address { get; set; }
}
Bulid the project and add new Controller (EmployeeController.cs).
Now you see the Create action in EmployeeController.cs:
public ActionResult Create()
{
return View();
}
//
// POST:
/Employee/Create
[HttpPost]
public ActionResult Create(Employee employee)
{
if (ModelState.IsValid)
{
db.Employees.Add(employee);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
We use the ModelState.IsValid
property to validate the data.
After that check the Create.cshtml
in View>> Employee folder.
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Employee</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MobileNo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MobileNo)
@Html.ValidationMessageFor(model => model.MobileNo)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back
to List", "Index")
</div>
@section
Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Build the project and
run.
No comments:
Post a Comment