views:

329

answers:

2

Hi,

I have a controller that sets TempData before returning a view,

public ActionResult Edit(int id, int? order)
{
    Route route = new Route();
    // Do Work
    TempData["Route"] = route;
    return View(new FormViewModel(route, obj1, obj2));
}

This view contians a partial view with a link which goes to another action method called delete, the code for the delete link is:

<%= Html.ActionLink("Delete", "Delete", new { order = item.Order })%>

The code for the Delete action method is:

public ActionResult Delete(int order)
{
    Route route = (Route)TempData["Route"];
    // Do Work
}

The problem that I'm having is when I try to get TempData["Route"]; from the Delete action method is returning null.

I'm wondering if the issue is that this is a Get and not a Post? If so how can I do a Post to the Delete ActionMethod from within my form?

+2  A: 

TempData persist between two requests. What does the ReturnView method in your Edit action returns? As far as I can tell it is not a standard method defined in the Controller class. Are you redirecting in this method (i.e. returning a RedirectToRouteResult)?

Also are there any other requests that could occur between your Edit and Delete actions? For example ajax requests.

Generally it is not a good idea to use TempData to persist something for a long time. The pattern usually is the following:

public ActionResult Update() 
{
    // When you put something into the TempData dictionary you usually
    // redirect immediately to an action that will use the object stored
    // inside.
    TempData["something"] = "something";
    return RedirectToAction("success");
}

public ActionResult Success() 
{
    var something = TempData["something"];
    return View();
}

If you need to persist something for a longer time you should use Session.

Darin Dimitrov
I've edited the post, it was a typo, should have been return View() not returnview.
Fermin
How about ajax requests that could invalidate TempData?
Darin Dimitrov
Fixed it, it was a problem with SessionState. Sorted it out with a few changes to the web.config.
Fermin
I take it back, I added SessionState mode="InProc" into the SessionState but it's changed my nice looking URL's from http://localhost:4402/Route/Edit/1 to: http://localhost:4402/(S(e23msb30lb3u4vzbhlkhkajx))/Route/Edit/1 Any ways round this? I don't have any AJAX requests that invalidate TempData.
Fermin
The "token" you see in the url is when you disable cookies for session tracking. Just set cookieless="false" in sessionState and your urls will be nice again.
Darin Dimitrov
When I set cookieless="false" the TempData is not persisted. I've checked browser settings to allow cookies. If I have cookieless="true" then the TempData is persisted, any reason you can think of?
Fermin
Install Firebug (if not already installed), open the "net" tab and perform the request. Inspect the headers. Is there a "ASP.NET_SessionId" cookie sent with your request?
Darin Dimitrov
A: 

Another factor of TempData not working is when your App is under a distributed system.

cqin