I have an employee class in model for ASP.NET MVC3. There is a field named “EmpName”. There is a create action for creating employee records. There should be a server side validation that - the second letter of the name should be “E” and third letter of the name should be “F”. (There is no client side validation). If the validation is failed, the message should be displayed in the create view as a validation summary. How do we do this?
Note: Validation errors for these two validations is expected to come as two error result (two different line).
Note: I am not using Entity Framework
Following is the view code.
@model MyApp.Employee
@{ ViewBag.Title = "Create"; }
<h2>Create</h2>
@using (Html.BeginForm())
{
<div > EmpName :~: @Html.EditorFor(model => model.EmpName) </div>
CONTROLLER
// GET:
public ActionResult Create()
{
return View();
}
// POST:
[HttpPost]
public ActionResult Create(Employee emp)
{
if (ModelState.IsValid)
{
//Save the employee in DB first and then redirectToAction.
return RedirectToAction("Index");
}
}
READING:
http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html
Add Sorting and Searching in Contact Management ASP.NET MVC Application
ValidationSummary and ValidationMessageFor with custom CSS shown when no errors present
http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-1
What is the better ASP.NET MVC 3.0 Custom Validation approach
http://dotnetslackers.com/articles/aspnet/Validating-Data-in-ASP-NET-MVC-Applications.aspx