public IActionResult EditLanguages(string buttonName, string languageName) { Action ac = () => { }; if (buttonName == "New") { if (string.IsNullOrEmpty(languageName)) { return(RedirectToAction("ErrorIndex", new { errorMsg = "Language name cannot be empty." })); } var lang = new Language(languageName); ac = () => LanguagesService.Add(lang); } else { var id = int.Parse(buttonName.Split("_")[1]); var lang = LanguagesService.Get(id); if (buttonName.StartsWith("Rename")) { if (string.IsNullOrEmpty(languageName)) { return(RedirectToAction("ErrorIndex", new { errorMsg = "Language name cannot be empty." })); } lang.Name = languageName; ac = () => LanguagesService.Update(lang); } else if (buttonName.StartsWith("Delete")) { try { LanguagesService.Remove(lang); } catch { return(RedirectToAction("ErrorIndex", new { errorMsg = "Cannot delete language because there are words still using it." })); } } } try { ac.Invoke(); } catch (Exception e) { return(RedirectToAction("ErrorIndex", new { errorMsg = "Something went wrong: " + e.Message })); } return(RedirectToAction("Index")); }
public ActionResult Create(LanguagesViewModels model) { if (!ModelState.IsValid) { Response.Headers["InvalidModel"] = "false"; SetViewBagsAndHeaders(Request.IsAjaxRequest(), "Add new language", "You have some validation errors."); if (Request.IsAjaxRequest()) { return(PartialView(model)); } return(View(model)); } if (User.Identity.GetActiveProject() == "0") { SetViewBagsAndHeaders(Request.IsAjaxRequest(), "Add new language", "You have to create project first."); ModelState.AddModelError("", "Create your first project and then you can add languages."); if (Request.IsAjaxRequest()) { return(PartialView(model)); } return(View(model)); } SetViewBagsAndHeaders(Request.IsAjaxRequest(), "All added languages", "New Language is created successfully."); model.IdProject = int.Parse(User.Identity.GetActiveProject()); model.DateChanged = DateTime.Now; model.DateCreated = DateTime.Now; model.UserName = User.Identity.Name; languagesService.Add(Mapper.Map <Languages>(model)); if (Request.IsAjaxRequest()) { return(PartialView("Index", languagesService.GetByActiveProject(UserActiveProject).ProjectTo <LanguagesViewModels>())); } return(View("Index", languagesService.GetByActiveProject(UserActiveProject).ProjectTo <LanguagesViewModels>())); }
public void Add(Languages obj) { _service.Add(obj); }