public ActionResult Save(StudentGroup model) { try { string msg = model.Validate(); if (!string.IsNullOrEmpty(msg)) { ViewBag.Message = msg; return(View("Edit", model)); } using (DataContext ctx = new DataContext()) { if (model.Id != 0) { var item = ctx.StudentGroup.FirstOrDefault(t => t.Id == model.Id); item.Name = model.Name; } else { ctx.StudentGroup.Add(model); } ctx.SaveChanges(); return(RedirectToAction("Index")); } } catch (Exception ex) { ViewBag.Message = ex.Message; return(Edit(model.Id)); } }