コード例 #1
0
        // Reusable Methods

        private DinnerFormViewModel CreateFormViewModel(Dinner dinner)
        {
            IEnumerable <Country> countries = _countryRepository.GetAllCountries();
            var viewModel = new DinnerFormViewModel(dinner, countries);

            return(viewModel);
        }
コード例 #2
0
        private ActionResult Save(DinnerFormViewModel viewModel)
        {
            Dinner dinner = viewModel.Dinner.ToModel(_dataContext);

            ViewResult viewResult = ModificationNotAuthorized(dinner);

            if (viewResult != null)
            {
                return(viewResult);
            }

            if (ModelState.IsValid && dinner.IsValid)
            {
                _dinnerRepository.Save();

                return(RedirectToAction(ActionNames.Details, new { id = dinner.DinnerID }));
            }
            else
            {
                IEnumerable <RuleViolation> ruleViolations = dinner.GetRuleViolations();
                ModelState.AddRuleViolations(ruleViolations);

                DinnerFormViewModel viewModel2 = CreateFormViewModel(dinner);
                return(View(viewModel2));
            }
        }
コード例 #3
0
        public ActionResult Edit(DinnerFormViewModel dinnerFormViewModel)
        {
            var dinner = iDinnerRepos.GetDinner(dinnerFormViewModel.DinnerId);

            if (!dinner.IsHostedBy(User.Identity.Name))
            {
                return(View("NotFound"));
            }

            try
            {
                UpdateModel(dinner);
                iDinnerRepos.Save();
                ViewBag.CountryID = new SelectList(iDinnerRepos.GetCountries(), "CountryID", "Name", dinner.CountryID);
                return(RedirectToAction("Details", new { id = dinner.DinnerId }));
            }
            catch
            {
                var dinnerModel = new DinnerFormViewModel()
                {
                    DinnerId     = dinnerFormViewModel.DinnerId,
                    Title        = dinnerFormViewModel.Title,
                    Address      = dinnerFormViewModel.Address,
                    ContactEmail = dinnerFormViewModel.ContactEmail,
                    ContactPhone = dinnerFormViewModel.ContactPhone,
                    EventDate    = dinnerFormViewModel.EventDate,
                    Latitude     = dinnerFormViewModel.Latitude,
                    Longitude    = dinnerFormViewModel.Longitude,
                    CountryID    = dinnerFormViewModel.CountryID,
                    Countries    = new SelectList(db.Countries, "CountryId", "Name")
                };
                return(View(dinnerModel));
            }
        }
コード例 #4
0
        // GET: Dinners/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Dinner dinner = iDinnerRepos.GetDinner(id ?? 0);

            if (dinner == null)
            {
                return(HttpNotFound());
            }
            var dinnerModel = new DinnerFormViewModel()
            {
                DinnerId     = dinner.DinnerId,
                Title        = dinner.Title,
                Address      = dinner.Address,
                ContactEmail = dinner.ContactEmail,
                ContactPhone = dinner.ContactPhone,
                EventDate    = dinner.EventDate,
                Latitude     = dinner.Latitude,
                Longitude    = dinner.Longitude,
                CountryID    = dinner.CountryID,
                Countries    = new SelectList(db.Countries, "CountryId", "Name")
            };

            return(View(dinnerModel));
        }
コード例 #5
0
        public ViewResult Edit(int id)
        {
            Dinner dinner = _dinnerRepository.GetDinner(id);

            ViewResult viewResult = ModificationNotAuthorized(dinner);

            if (viewResult != null)
            {
                return(viewResult);
            }

            DinnerFormViewModel viewModel = CreateFormViewModel(dinner);

            return(View(viewModel));
        }
コード例 #6
0
        public ViewResult Create()
        {
            var userName = User.Identity.Name;

            var dinner = new Dinner
            {
                HostedBy  = userName,
                EventDate = DateTime.Now.AddDays(7)
            };

            var rsvp = new RSVP
            {
                AttendeeName = userName,
                Dinner       = dinner
            };

            DinnerFormViewModel viewModel = CreateFormViewModel(dinner);

            return(View(viewModel));
        }
コード例 #7
0
        public ActionResult Create()
        {
            var dinner = new Dinner
            {
                EventDate = DateTime.Today
            };
            var dinnerModel = new DinnerFormViewModel()
            {
                DinnerId     = dinner.DinnerId,
                Title        = dinner.Title,
                Address      = dinner.Address,
                ContactEmail = dinner.ContactEmail,
                ContactPhone = dinner.ContactPhone,
                EventDate    = dinner.EventDate,
                Latitude     = dinner.Latitude,
                Longitude    = dinner.Longitude,
                CountryID    = dinner.CountryID,
                Countries    = new SelectList(db.Countries, "CountryId", "Name")
            };

            return(View(dinnerModel));
        }
コード例 #8
0
        public ActionResult Create(DinnerFormViewModel dinnerFormViewModel)
        {
            if (ModelState.IsValid)
            {
                var dinner = new Dinner()
                {
                    DinnerId     = dinnerFormViewModel.DinnerId,
                    Title        = dinnerFormViewModel.Title,
                    EventDate    = dinnerFormViewModel.EventDate,
                    ContactEmail = dinnerFormViewModel.ContactEmail,
                    ContactPhone = dinnerFormViewModel.ContactPhone,
                    Address      = dinnerFormViewModel.Address,
                    CountryID    = dinnerFormViewModel.CountryID,
                    Latitude     = dinnerFormViewModel.Latitude,
                    Longitude    = dinnerFormViewModel.Longitude
                };
                var countryForDinner = db.Countries.FirstOrDefault(c => c.CountryID == dinnerFormViewModel.CountryID);
                dinner.Country = countryForDinner;
                db.Dinners.Add(dinner);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            var dinnerModel = new DinnerFormViewModel()
            {
                DinnerId     = dinnerFormViewModel.DinnerId,
                Title        = dinnerFormViewModel.Title,
                Address      = dinnerFormViewModel.Address,
                ContactEmail = dinnerFormViewModel.ContactEmail,
                ContactPhone = dinnerFormViewModel.ContactPhone,
                EventDate    = dinnerFormViewModel.EventDate,
                Latitude     = dinnerFormViewModel.Latitude,
                Longitude    = dinnerFormViewModel.Longitude,
                CountryID    = dinnerFormViewModel.CountryID,
                Countries    = new SelectList(db.Countries, "CountryId", "Name")
            };

            return(View(dinnerModel));
        }
コード例 #9
0
        // GET: Dinners/Details/5
        public ActionResult Details(int?id)
        {
            Dinner dinner = iDinnerRepos.GetDinner(id ?? 0);

            if (dinner == null)
            {
                return(View("NotFound"));
            }
            var dinnerModel = new DinnerFormViewModel()
            {
                DinnerId     = dinner.DinnerId,
                Title        = dinner.Title,
                Address      = dinner.Address,
                ContactEmail = dinner.ContactEmail,
                ContactPhone = dinner.ContactPhone,
                EventDate    = dinner.EventDate,
                Latitude     = dinner.Latitude,
                Longitude    = dinner.Longitude,
                CountryID    = dinner.CountryID,
                Countries    = new SelectList(db.Countries, "CountryId", "Name")
            };

            return(View(dinnerModel));
        }
コード例 #10
0
 public ActionResult Create(DinnerFormViewModel viewModel)
 {
     return(Save(viewModel));
 }