public ActionResult EditLanguage(LanguageEditModel model) { if (ModelState.IsValid) { try { string image = ""; string sound = ""; var js = new JavaScriptSerializer(); if (model.Id == Guid.Empty) { //save image and sound if (model.Sound != null) { sound = Helper.GenerateUniqueNumberic() + model.Sound.FileName.Substring(model.Sound.FileName.LastIndexOf('.')); model.Sound.SaveAs(HttpContext.Server.MapPath("~/Resources/Sounds/"+sound)); } if (model.Image != null) { image = Helper.GenerateUniqueNumberic() + model.Image.FileName.Substring(model.Image.FileName.LastIndexOf('.')); model.Image.SaveAs(HttpContext.Server.MapPath("~/Resources/Images/" + image)); } var lang = new Language() { CategoryId = model.CategoryId, Description = model.Description, Id = Guid.NewGuid(), Image = image, KeyLanguage = model.KeyLanguage, Sound = sound, ValueLanguage = js.Serialize(model.ValueLanguage) }; _bhtaEntities.Languages.Add(lang); var result = _bhtaEntities.SaveChanges(); if (result > 0) return RedirectToAction("language", "home"); ModelState.AddModelError("","Thêm mới từ vựng không thành công"); } else { var lang = _bhtaEntities.Languages.SingleOrDefault(l => l.Id == model.Id); if(lang == null) return RedirectToAction("language", "home"); //save image and sound if (model.Sound != null) { sound = Helper.GenerateUniqueNumberic() + model.Sound.FileName.Substring(model.Sound.FileName.LastIndexOf('.')); model.Sound.SaveAs(HttpContext.Server.MapPath("~/Resources/Sounds/" + sound)); } if (model.Image != null) { image = Helper.GenerateUniqueNumberic() + model.Image.FileName.Substring(model.Image.FileName.LastIndexOf('.')); model.Image.SaveAs(HttpContext.Server.MapPath("~/Resources/Images/" + image)); } lang.CategoryId = model.CategoryId; lang.Description = model.Description; lang.ValueLanguage = js.Serialize(model.ValueLanguage); lang.KeyLanguage = model.KeyLanguage; if (sound != "") lang.Sound = sound; if (image != "") lang.Image = image; _bhtaEntities.SaveChanges(); return RedirectToAction("language", "home"); } } catch (Exception exception) { ModelState.AddModelError("", exception.ToString()); } } model.Category = _bhtaEntities.Categories.ToList() .Select( m => new SelectListItem() {Value = m.Id.ToString(), Selected = model.CategoryId == m.Id, Text = m.Name}) .ToList(); return View(model); }
public ActionResult EditLanguage(Guid? id) { LanguageEditModel model; if (id == null || id == Guid.Empty) { model = new LanguageEditModel { Category = _bhtaEntities.Categories.ToList() .Select( m => new SelectListItem() {Value = m.Id.ToString(), Selected = id == m.Id, Text = m.Name}) .ToList() }; return View(model); } var item = _bhtaEntities.Languages.SingleOrDefault(m => m.Id == id); if (item == null) return RedirectToAction("language", "home"); var js = new JavaScriptSerializer(); model = new LanguageEditModel { CategoryId = item.CategoryId, Description = item.Description, Id = item.Id, KeyLanguage = item.KeyLanguage, ValueLanguage = js.Deserialize<LanguageName>(item.ValueLanguage), ImageName = item.Image }; model.Category = _bhtaEntities.Categories.ToList() .Select(m => new SelectListItem() {Value = m.Id.ToString(), Selected = id == m.Id, Text = m.Name}) .ToList(); return View(model); }