0

I'm implementing an ASP.NET MVC post/redirect/getpattern in an Azure website. When a user creates a new entity they are redirected from the create view to the edit view with the new object id as part of the url.

The entity has quite a few fields so it's not uncommon to save multiple times and to reassure the user that their data is being saved we are showing a 'Saved successfully' message using javascript.

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Branch branch, int orgs)
    {
        if (ModelState.IsValid)
        {
            // model is valid, save it and redirect to edit
            _branchRepository.Save(branch);
            TempData["Message"] = new NotificationViewModel(NotificationSeverity.Success, 
                                  "Saved", 
                                  "Saved successfully");
            return RedirectToAction("Edit", new { id = branch.Id });
        }

        // model is invalid, don't save it, let them have another go
        TempData["Message"] = new NotificationViewModel(NotificationSeverity.Warning, 
                              "I'm sorry, Dave.", 
                              "I'm afraid I can't do that.");
        ModelState.Clear();
        return View("Edit", branch);
    }  

My understanding of TempData is that any data stored in TempData will be around for the life the current request and the next request only (or until the item is removed explicitly) and is the best place to put data you want to pass another view that you will be redirecting to.

Is TempData the best place for this message?

Note: I've read that if you're load balancing your webservers that you have to have Sticky Sessions enabled. Does Azure turn on Sticky Sessions automatically or do you need to manually configure this?

Derek Tomes
  • 3,989
  • 3
  • 27
  • 41
  • 1
    That's a good question about Azure and the Get. To be safe I tend to pass an optional parameter to the Redirect method rather than use TempData. I'll be interested to see what others post. – Neil Thompson Feb 02 '15 at 21:30
  • By default TempData is based on session but you can provide custom implementation of TempDataProvider which can store parameters in URL, Cookies or somewhere else – Sławomir Rosiek Feb 02 '15 at 21:37
  • Neil, does the optional parameter ends up in the url? If so, and I bookmark that url, wouldn't that cause the 'saved successfully' message to display every time I open that bookmark? – Derek Tomes Feb 02 '15 at 21:43

1 Answers1

1

Storing validation message betweeen requests when using PRG pattern in TempData is in my opinion the most popular usage of TempData. Additionally you can write some action filters that will automatically store all modelstate in tempdata if you return Redirect result and move that data from tempdata to modelstate/viewstate if you return view in Get phase.

In MVC Contrib there are two such filters: http://mvccontrib.codeplex.com/SourceControl/latest#src/MVCContrib/Filters/ModelStateToTempDataAttribute.cs http://mvccontrib.codeplex.com/SourceControl/latest#src/MVCContrib/Filters/TempDataToViewData.cs

Data stored in TempData is removed after end of the request in which it was read until you call TempData.Keep(key).

Sławomir Rosiek
  • 4,038
  • 3
  • 25
  • 43