public ActionResult Add(BusinessPartnerBindingModel model) { //check if model is valid, if not retrn to Index and dispay error message if (!this.ModelState.IsValid) { TempData["class"] = "alert-danger"; TempData["Response-bad"] = "Unable to add new Business Partner "; return(RedirectToAction("Index")); } //check if entity with current name exists if (_bussinessPartner.All().Any(i => i.Name == model.Name)) { TempData["class"] = "alert-danger"; TempData["Response"] = "Bad Request! Unvalid Business Partner should be unique!"; return(RedirectToAction("Index")); } //project to db model and create it var partner = new BusinessPartner() { Email = model.Email, Name = model.Name, PhoneNumber = model.PhoneNumber, }; _bussinessPartner.Add(partner); return(RedirectToRoute("BusinessPartner")); }
public ActionResult Edit(BusinessPartnerBindingModel model) { //check if model state is valid, if not redirect to Edit/{id} + returns a message if (!this.ModelState.IsValid) { TempData["Response"] = "Unable to Save Changes. Try again !"; TempData["class"] = "alert-danger"; return(RedirectToAction("Edit", new { id = model.Id })); } //check if an entity with a inputed name is existing if (_bussinessPartner.All().Any(i => i.Name == model.Name)) { TempData["class"] = "alert-danger"; TempData["Response"] = "Bad Request! Unvalid Input Username should be unique!"; return(RedirectToAction("Index")); } // project a new entity with given information var partner = new BusinessPartner() { Email = model.Email, Name = model.Name, PhoneNumber = model.PhoneNumber, Id = model.Id }; //update that entity in DB _bussinessPartner.Update(partner); TempData["class"] = "alert-success"; TempData["Response"] = "Successfully updated that entity!"; return(RedirectToAction("Edit", new { id = model.Id })); }
/// <summary> /// Get Method | Route: /BusinessPartner | View of all business partner and input for create a new one /// </summary> /// <returns></returns> public ActionResult Index() { //Take all entities from db and project them into view model var allPartners = _bussinessPartner.All().Select(i => new BusinessPartnerDetailsView { Email = i.Email, Id = i.Id, Name = i.Name, PhoneNumber = i.PhoneNumber, }).OrderByDescending(i => i.Id).ToList(); var addbox = new BusinessPartnerBindingModel(); var model = new BusinessPartnerGlobalView() { AddBusinessPartner = addbox, PartnerDetails = allPartners }; return(View(model)); }