Esempio n. 1
0
        public async Task <ActionResult> EditService(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            if (id == -1)
            {
                ViewBag.Title = "New Service";
                var NewServiceModel = new ProfileServiceEditModel
                {
                    Title           = "",
                    Description     = "",
                    CreditWorth     = 0,
                    IsActive        = Service.ServiceStatus.Active,
                    Location        = "",
                    _viewStatusList = new List <Service.ServiceStatus> {
                        Service.ServiceStatus.Active, Service.ServiceStatus.Inactive
                    },
                    ImagePath = null
                };
                return(View(NewServiceModel));
            }
            ViewBag.Title = "Edit Service";
            string          currentUserId = User.Identity.GetUserId();
            ApplicationUser currentUser   = await UserManager.FindByIdAsync(currentUserId);

            IList <Service> userServices = currentUser.Services.Where(s => s.IsActive != Service.ServiceStatus.Deleted).ToList();
            var             service      = userServices.FirstOrDefault(s => s.Id as int? == id);

            if (service == null)
            {
                return(HttpNotFound());
            }
            var serviceModel = new ProfileServiceEditModel
            {
                Title           = service.Title,
                Description     = service.Description,
                CreditWorth     = service.CreditWorth,
                IsActive        = service.IsActive,
                Location        = service.Location,
                _viewStatusList = new List <Service.ServiceStatus> {
                    Service.ServiceStatus.Active, Service.ServiceStatus.Inactive
                }
                //ImagePath = service.ImagePath
            };

            return(View(serviceModel));
        }
Esempio n. 2
0
        public async Task <ActionResult> EditService(ProfileServiceEditModel model)
        {
            Service service = new Service()
            {
                SupplierId = user.Id
            };

            if (model.Id != -1)
            {
                service = await db.Services.FirstOrDefaultAsync(s => s.Id == model.Id);
            }

            if (ModelState.IsValid)
            {
                service.Title       = model.Title;
                service.Description = model.Description;
                service.CreditWorth = model.CreditWorth;
                service.Location    = model.Location;
                service.IsActive    = model.IsActive;
                if (model.ImagePath != null && model.ImagePath.ContentLength > 0)
                {
                    string serverPath = "~/Assets";

                    string path = Path.Combine(Server.MapPath(serverPath), user.Id + Path.GetFileName(model.ImagePath.FileName));
                    model.ImagePath.SaveAs(path);
                    service.ImagePath = serverPath + "/" + user.Id + Path.GetFileName(model.ImagePath.FileName);
                }
                if (model.Id == -1)
                {
                    db.Services.Add(service);
                }
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(model));
        }