コード例 #1
0
        public ActionResult UpdateDetails()
        {
            var userName = this.User.Identity.GetUserName();

            ViewBag.UserName = userName;

            var user = _CreateUserModel(userName);

            var allClubs     = this.clubService.GetAllNames();
            var allCities    = this.cityService.GetAllNames();
            var allPlayTimes = this.playTimeService.GetAllTimes();

            var viewDetails = new UpdateDetailsBindModel()
            {
                AllPlayTimes = allPlayTimes,
                AllClubs     = allClubs,
                AllCities    = allCities,
                City         = user.City,
                Age          = user.Age,
                Gender       = user.Gender,
                Club         = user.Club,
                PlayTime     = user.PlayTime,
                SkillLevel   = user.Skill,
                Info         = user.Info,
            };

            return(View(viewDetails));
        }
コード例 #2
0
        public ActionResult UpdateDetails(UpdateDetailsBindModel model)
        {
            var id   = this.User.Identity.GetUserId();
            var user = this.userService
                       .GetAll()
                       .Where(u => u.Id == id)
                       .First();

            var detailsId = this.User.Identity.GetDetailsId();

            var details = this.detailsService
                          .GetAll()
                          .Where(x => x.Id.ToString() == detailsId)
                          .First();

            if (model.Club != null)
            {
                var club = this.clubService
                           .GetAll()
                           .Where(x => x.Name == model.Club)
                           .First();

                if (club == null)
                {
                    return(this.ClubValidationFailure(model.Club));
                }

                details.Club_Id = club.Id;
            }

            if (model.PlayTime != null)
            {
                var time = this.playTimeService
                           .GetAll()
                           .Where(x => x.Time.ToString() == model.PlayTime)
                           .First();

                details.PlayTime_Id = time.Id;
            }

            if (model.City != null && model.City.Length > 2)
            {
                var city = this.cityService
                           .GetAll()
                           .Where(x => x.Name == model.City)
                           .FirstOrDefault();

                if (city == null)
                {
                    city = new City()
                    {
                        Name = model.City
                    };

                    this.cityService.Add(city);
                }

                details.City_Id = city.Id;
            }

            if (model.Age > 0)
            {
                details.Age = model.Age;
            }

            if (model.Info != null)
            {
                details.Info = model.Info;
            }


            if (model.Gender != null)
            {
                details.Gender = (Gender)Enum.Parse(typeof(Gender), model.Gender);
            }

            if (model.SkillLevel >= 1)
            {
                details.Skill = model.SkillLevel;
            }
            else
            {
                details.Skill = 1;
            }


            if (model.Image != null && this.ModelState.IsValid)
            {
                ViewBag.ImageError = null;
                var fileName = Path.GetFileName(model.Image.FileName);
                var path     = Path.Combine(Server.MapPath("~/Content/Uploaded"), fileName);
                model.Image.SaveAs(path);
                details.ImageUrl = $"/Content/Uploaded/{fileName}";
            }
            else
            {
                details.ImageUrl = "/Content/Images/tennis-default.png";
            }


            var result = this.detailsService.Update(details);

            if (result > 0)
            {
                return(this.RedirectToAction("Details"));
            }

            return(this.DetailsValidationFailure());
        }