Esempio n. 1
0
        // GET : /Vehicle/Add
        public ActionResult Add()
        {
            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;
                var currentUsers = Session["UserData"] as Admin;
                var dealers = GetDealers(currentUsers.UserId.ToString());
                var dealerSelectList =
                    new SelectList(
                        dealers.Select(
                            deal => new SelectListItem() { Text = deal.Username, Value = deal.UserId.ToString() }), "Value",
                        "Text");

                ViewData["dealerList"] = dealerSelectList;

                var customers = GetCustomers(currentUsers.UserId.ToString());
                var customerSelectList =
                    new SelectList(
                        dealers.Select(
                            deal => new SelectListItem() { Text = deal.Username, Value = deal.UserId.ToString() }), "Value",
                        "Text");

                ViewData["customerList"] = dealerSelectList;

            }

            //return View(new VehicleViewModel());
            VehicleViewModel vehicle = new VehicleViewModel();

            if (Request.IsAjaxRequest())
            {
                return PartialView("_Add", vehicle);
            }
            return View(vehicle);
        }
Esempio n. 2
0
        public ActionResult Add(VehicleViewModel module)
        {
            HttpPostedFileBase file = Request.Files["fileUpload"];
            if (ModelState.IsValid)
            {
                try
                {
                    var vehicle = Mapper.Map<VehicleViewModel, Vehicle>(module);
                    if (Session["UserData"] is Distributor)
                    {
                        vehicle.CustomerId = ((Distributor)Session["UserData"]).UserId;
                    }
                    else
                    {
                        vehicle.CustomerId = Guid.Parse(Request.Form["customerId"]);
                        vehicle.ImageName = file.FileName;
                        vehicle.Image = ConvertToBytes(file);
                    }
                    _vehicleService.CreateVehicle(vehicle);
                    _vehicleService.SaveVehicle();

                    return RedirectToAction("Index", "Vehicle");
                }
                catch (InvalidOperationException ex)
                {
                    ModelState.AddModelError("Error", ex.Message);
                }

                catch (Exception ex)
                {
                    ModelState.AddModelError("Error", ex.Message);
                }
            }

            if (Request.IsAjaxRequest())
                return PartialView("_Add", module);

            return View(module);
        }
Esempio n. 3
0
        public ActionResult Edit(VehicleViewModel module)
        {
            HttpPostedFileBase file = Request.Files["fileUpload"];
            //model.Password = "******";
            if (Session["UserData"] is Admin)
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        if (module.VehicleId == Guid.Empty)
                        {

                            module.VehicleId = Guid.NewGuid();
                            var vehicle = Mapper.Map<VehicleViewModel, Vehicle>(module);
                            vehicle.CustomerId = Guid.Parse(Request.Form["custId"]);
                            if (file != null)
                            {
                                vehicle.ImageName = file.FileName;
                                vehicle.Image = ConvertToBytes(file);
                            }
                            _vehicleService.CreateVehicle(vehicle);
                            _vehicleService.SaveVehicle();

                        }
                        else
                        {
                            var vehicle = _vehicleService.GetVehicle(module.VehicleId);
                            if (file != null)
                            {
                                vehicle.ImageName = file.FileName;
                                vehicle.Image = ConvertToBytes(file);
                            }
                            Mapper.Map<VehicleViewModel, Vehicle>(module, vehicle);
                            _vehicleService.UpdateVehicle(vehicle);
                            _vehicleService.SaveVehicle();
                        }

                        return Json(new { Result = "Success" });
                    }
                    catch (Exception ex)
                    {
                        Logger.ErrorFormat("Controller : {0} - Action : {1}, Message : {2}", this.ControllerContext.RouteData.Values["controller"].ToString(), this.ControllerContext.RouteData.Values["action"].ToString(), ex.Message);

                    }
                }

                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", module.CustomerId.ToString());

                ViewData["DistributorList"] = distributorsSelectList;
                var dealers = GetDealers(currentUser.UserId.ToString());
                var dealersSelectList = new SelectList(
                                             dealers.Select(deal => new SelectListItem() { Text = deal.Username, Value = deal.UserId.ToString() }), "Value", "Text");
                ViewData["DealerList"] = dealersSelectList;

                var customers = GetCustomers(currentUser.UserId.ToString());
                var customersSelectList = new SelectList(
                                             customers.Select(cust => new SelectListItem() { Text = cust.Username, Value = cust.UserId.ToString() }), "Value", "Text");
                ViewData["CustomerList"] = customersSelectList;

            }
            if (Request.IsAjaxRequest())
                return PartialView("_Edit", module);
            return View(module);
        }
Esempio n. 4
0
        // GET : /Distributor/Edit
        public ActionResult Edit(string vehicleId)
        {
            VehicleViewModel customerVM = new VehicleViewModel();
            if (Session["UserData"] is Admin)
            {
                if (string.IsNullOrWhiteSpace(vehicleId))
                {
                    customerVM.VehicleId = Guid.Empty;
                }
                else
                {
                    var vehicleGuid = Guid.Parse(vehicleId);
                    Vehicle vehicle = _vehicleService.GetVehicle(vehicleGuid);
                    customerVM = Mapper.Map<Vehicle, VehicleViewModel>(vehicle);
                }
                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;

                var dealers = GetDealers(currentUser.UserId.ToString());
                var dealersSelectList = new SelectList(
                                             dealers.Select(deal => new SelectListItem() { Text = deal.Username, Value = deal.UserId.ToString() }), "Value", "Text");
                ViewData["DealerList"] = dealersSelectList;

                var customers = GetCustomers(currentUser.UserId.ToString());
                var customersSelectList = new SelectList(
                                             customers.Select(cust => new SelectListItem() { Text = cust.Username, Value = cust.UserId.ToString() }), "Value", "Text");
                ViewData["CustomerList"] = customersSelectList;
            }
            return PartialView("_Edit", customerVM);
        }