public ActionResult Add(DealerViewModel model) { HttpPostedFileBase file = Request.Files["fileUpload"]; if (ModelState.IsValid) { try { var dealer = Mapper.Map<DealerViewModel, Dealer>(model); if (Session["UserData"] is Distributor) { dealer.Parent = ((Distributor)Session["UserData"]).UserId; } else { dealer.Parent = Guid.Parse(Request.Form["distributorId"]); dealer.ImageName = file.FileName; dealer.Logo = ConvertToBytes(file); } _dealerService.CreateDealer(dealer); _dealerService.SaveDealer(); return RedirectToAction("Index", "Dealer"); } catch (InvalidOperationException ex) { ModelState.AddModelError("Error", ex.Message); } catch (Exception ex) { ModelState.AddModelError("Error", ex.Message); } } if (Request.IsAjaxRequest()) return PartialView("_Add", model); return View(model); }
public ActionResult Edit(DealerViewModel model) { HttpPostedFileBase file = Request.Files["fileUpload"]; model.Password = "******"; if (Session["UserData"] is Admin) { if (ModelState.IsValid) { try { if (model.UserId == Guid.Empty) { model.UserId = Guid.NewGuid(); var dealer = Mapper.Map<DealerViewModel, Dealer>(model); if (file != null) { dealer.ImageName = file.FileName; dealer.Logo = ConvertToBytes(file); } _dealerService.CreateDealer(dealer); _dealerService.SaveDealer(); } else { var dealer = _dealerService.GetUser(model.UserId); Mapper.Map<DealerViewModel, Dealer>(model, dealer); if (file != null) { dealer.ImageName = file.FileName; dealer.Logo = ConvertToBytes(file); } _dealerService.UpdateUser(dealer); _dealerService.SaveDealer(); } return Json(new { Result = "Success" }); } catch (Exception ex) { } } var currentUser = Session["UserData"] as Admin; var distributors = GetDistributors(currentUser.UserId.ToString()); var distributorsSelectList = new SelectList( distributors.Select( dist => new SelectListItem() { Text = dist.Username, Value = dist.UserId.ToString() }), "Value", "Text", model.Parent.ToString()); ViewData["DistributorList"] = distributorsSelectList; } return PartialView("_Edit", model); }
// GET : /Distributor/Edit public ActionResult Edit(string dealerId) { DealerViewModel dealerVM = new DealerViewModel(); if (Session["UserData"] is Admin) { var currentUser = Session["UserData"] as Admin; var distributors = GetDistributors(currentUser.UserId.ToString()); var distributorsSelectList = new SelectList( distributors.Select( dist => new SelectListItem() { Text = dist.Username, Value = dist.UserId.ToString() }), "Value", "Text"); ViewData["DistributorList"] = distributorsSelectList; if (string.IsNullOrWhiteSpace(dealerId)) { dealerVM.UserId = Guid.Empty; } else { var dealerGuid = Guid.Parse(dealerId); Dealer dealer = _dealerService.GetUser(dealerGuid); dealerVM = Mapper.Map<Dealer, DealerViewModel>(dealer); } } return PartialView("_Edit", dealerVM); }