why we use this code in mvc entity framewirk
// GET: /Student/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Student/Create
[HttpPost] [ValidateAntiForgeryToken]
public ActionResult Create(Student student)
{
if (ModelState.IsValid)
{
db.Students.Add(student);
db.SaveChanges();
return RedirectToAction( "Index" );
}
return View(student);
}
//
what is meant by model state.isvalid why we check before to add student.
and why we use save changes() method here?

Rajkiran SwainPosted Jun 20, 2017, 2:50 PM
Abhishek SharmaPosted Jun 20, 2017, 12:25 PM
ModelState.IsValidtells you if any model errors have been added toModelState.The default model binder will add some errors for basic type conversion issues (for example, passing a non-number for something which is an "int"). You can populate ModelState more fully based on whatever validation system you're using.
The sample
DataAnnotationsmodel binder will fill model state with validation errors taken from theDataAnnotationsattributes on your model.