I have notice something special in asp.net mvc.
I did not specific any validation for DateTime. However, when i press the create button, it will auto display the error message if it is left empty or the format is not compatible with DateTime format. Is this predefined in ASP.NET MVC?
AFAIK, it is not available in ASP.NET MVC3? I am now currently using MVC 5. Which version is it available?
Secondly, I would like to know what is the use of ModelState.IsValid in there and what is the use of it?
This is because i notice that the model state is always valid, if it not valid, the create post method would not be invoked as the error message will be displayed beforehand.
public class Employee
{
public int ID { get; set; }
[Required]
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public DateTime DateOfBirth { get; set; }
}
[HttpPost]
[ActionName("Create")]
public ActionResult Create_Post()
{
Employee employee = new Employee();
UpdateModel(employee);
if (ModelState.IsValid)
{
EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer();
employeeBusinessLayer.AddEmployee(employee);
return RedirectToAction("Index");
}
return View();
}