public ActionResult UpdateStudent(StudentUpdateModel model, int? id) { var hallId = 0; var selectedHallId = 0; if (ModelState.IsValid) { var si = _db.StudentInformations.Find(id); if (model.HallName != null) { hallId = Convert.ToInt32(model.HallName); si.HallId = hallId; } si.Phone = model.Phone; si.Email = model.Email; si.PresentAddress = model.PresentAddress; _db.Entry(si).State = EntityState.Modified; _db.SaveChanges(); return RedirectToAction("DisplayAdminHomePage"); } ViewBag.halls = _db.Halls.Select(m => m.HallName).ToList(); var supm = new StudentUpdateModel(); var information = _db.StudentInformations.Find(id); supm.PresentAddress = information.PresentAddress; supm.Phone = information.Phone; supm.Email = information.Email; supm.Image = information.StudentImage; if (model.HallName != "") { selectedHallId = hallId; ViewBag.hallListShowId = 1; } var halls = _db.Halls.Select(c => new { HallId = c.Id, Hall = c.HallName }).ToList(); var selectList = new SelectList(halls, "HallId", "Hall", selectedHallId); ViewData["Halls"] = selectList; return PartialView("_UpdateStudent", supm); }
public ActionResult UpdateStudent(int id) { if (Request.IsAjaxRequest()) { var hallName = ""; ViewBag.halls = _db.Halls.Select(m => m.HallName).ToList(); var supm = new StudentUpdateModel(); var u = _db.StudentInformations.SingleOrDefault(m => m.Id.Equals(id)); var hallId = u.HallId; var n = _db.Halls.SingleOrDefault(c => c.Id.Equals(hallId.Value)); if (n != null) hallName = n.HallName; if (hallName != "") { ViewBag.hallListShowId = 1; } var information = _db.StudentInformations.Find(id); supm.PresentAddress = information.PresentAddress; supm.Phone = information.Phone; supm.Email = information.Email; supm.Image = information.StudentImage; var halls = _db.Halls.Select(c => new { HallId = c.Id, Hall = c.HallName }).ToList(); var h = _db.Halls.FirstOrDefault(m => m.HallName.Equals(hallName)); SelectList selectList; if (u.HallId > 0) { selectList = h != null ? new SelectList(halls, "HallId", "Hall", h.Id) : new SelectList(halls, "HallId", "HallName"); } else { selectList = new SelectList(halls, "HallId", "Hall"); } ViewData["Halls"] = selectList; return PartialView("_UpdateStudent", supm); } else { return View("ProcessingError"); } }