Exemplo n.º 1
0
        public ActionResult WithPRGandTempData(PRGViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Perform update
                if (model.DummyID.HasValue)
                {
                    DummyService.Edit(model.DummyID.Value, model.Name, model.Date, model.EmailAddress);
                    AddSuccessMessage("Edited successfully");
                }
                else
                {
                    model.DummyID = DummyService.Add(model.Name, model.Date, model.EmailAddress);
                    AddSuccessMessage("Added successfully");
                }

                // Follow Post-Redirect-Get pattern by redirecting to GET after successful POST update
                // Passing reload details via TempData.Set<T> for reloading in GET
                // (since the reload details is just an id, you should actually do this via the route approach - using TempData for the sake of the example)
                // NOTE: You should only use TempData when there are a lot of reload details to pass that aren't suitable for passing via route
                // NOTE: Only store the data necessary for reloading the object in TempData after the redirect - Don't store the object itself that you plan to reload
                TempData.Set <long?>("PRG", model.DummyID);

                return(RedirectToAction("WithPRGandTempData"));
            }

            return(View(model));
        }
Exemplo n.º 2
0
        public ActionResult WithoutPRG(long?id)
        {
            var model = new PRGViewModel();

            if (id.HasValue)
            {
                var dummy = DummyService.Get(id.Value);

                model.DummyID      = dummy.DummyID;
                model.Name         = dummy.Name;
                model.Date         = dummy.Date;
                model.EmailAddress = dummy.EmailAddress;
            }

            return(View(model));
        }
Exemplo n.º 3
0
        public ActionResult WithPRGandTempData(long?id)
        {
            var model = new PRGViewModel();

            if (!id.HasValue)
            {
                id = TempData.Get <long?>("PRG");
            }

            if (id.HasValue)
            {
                var dummy = DummyService.Get(id.Value);

                model.DummyID      = dummy.DummyID;
                model.Name         = dummy.Name;
                model.Date         = dummy.Date;
                model.EmailAddress = dummy.EmailAddress;
            }

            return(View(model));
        }
Exemplo n.º 4
0
        public ActionResult WithoutPRG(PRGViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Perform update
                if (model.DummyID.HasValue)
                {
                    DummyService.Edit(model.DummyID.Value, model.Name, model.Date, model.EmailAddress);
                    AddSuccessMessage("Edited successfully");
                }
                else
                {
                    model.DummyID = DummyService.Add(model.Name, model.Date, model.EmailAddress);
                    AddSuccessMessage("Added successfully");
                }

                // Not following Post-Redirect-Get often requires modifying the model state
                ModelState.Remove("DummyID");
            }

            return(View(model));
        }
Exemplo n.º 5
0
        public ActionResult WithPRG(PRGViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Perform update
                if (model.DummyID.HasValue)
                {
                    DummyService.Edit(model.DummyID.Value, model.Name, model.Date, model.EmailAddress);
                    AddSuccessMessage("Edited successfully");
                }
                else
                {
                    model.DummyID = DummyService.Add(model.Name, model.Date, model.EmailAddress);
                    AddSuccessMessage("Added successfully");
                }

                // Follow Post-Redirect-Get pattern by redirecting to GET after successful POST update
                // Pass reload details via route for reloading in GET (this is always the preferred approach)
                return(RedirectToAction("WithPRG", new { id = model.DummyID }));
            }

            return(View(model));
        }