TempData not carrying over during RedirectToAction

烈酒焚心 提交于 2021-02-04 17:21:10

问题


I have an interesting problem with the TempData object not passing values to another controller.

I set TempData["Enroll"] in the Enroll Controller's HttpPost method to an Enroll Model. I then read the TempData["Enroll"] object in the Register Controller's HttpGet method, but is empty/null.

I need to persist all of this data across 3 controllers.

Any thoughts?

Here is a code Snippet

//EnrollController.cs
[HttpPost]
public ActionResult Index(EnrollModel model)
{
   // ...
   TempData["EnrollModel"] = model;
   return RedirectToAction("Index", "Register");
}

// RegisterController.cs
public ActionResult Index(string type)
{
    RegisterModel model = new RegisterModel();

    EnrollModel enrollModel = TempData["EnrollModel"] as EnrollModel;
    model.ClientType = enrollModel.ClientType;
    // ...
}

回答1:


I have come across these sorts of limitations with TempData before. I found it unrealiable and sporadic at best.

You need to consider what you are trying to achieve. If you do need to store data, in practice the best place to do this is in a db (or store of sorts) it might seem a bit overkill but that is their purpose.

Two other points:

  1. Someone can hit your RegisterController Index method without going to the other before, in which case your code would break.

  2. If you are doing a multiple wizard style process, why not store the data in its partial state in the db, and complete the process only on the last screen? In this way no matter, where they stop/start or pick it up again, you will always know where they are in the process.




回答2:


I've had an issue where TempData got lost during the redirect on my local machine.

I've checked web.config sessionState Setting which was InProc and therefore no problem.

It turned out that I got another setting in web.config, which was taken from production system. It looked like this:

<httpCookies requireSSL="true" />

After turning the requireSSL to false TempData workes fine.




回答3:


I had the same problem today.

In this link some guys explain that RedirectAction method returns a HTTP 302 status to the browser, which causes the browser to make a new request and clear the temp, but I tried returning HTTP methods 303 (which is what the RedirectAction should be returning) and 307 also, and it didn't solve anything.

The only way of fixing the issue of TempData in my case was changing the sessionState directive of web.config to use StateServer instead of the default InProc. i.e:

<system.web>
    <sessionState mode="StateServer" cookieless="AutoDetect" timeout="30" stateConnectionString="tcpip=localhost:42424"></sessionState>
    ...
</system.web>

I figured this out when reading this Greg Shackles' article, where he explains how TempData works and build a custom TempDataProvider, which rely on MongoDB database instead of session like the default one.

Hope that my 4 hours researching helps someone to not waste their time.




回答4:


Save your results to a cache or db or pass in as posts/querystrings between your controllers. TempData is cleared by several things including a worker process reset which could surely happen between steps.

In addition your code above could get a null ref exception:

EnrollModel enrollModel = TempData["EnrollModel"] as EnrollModel;
if(enrollModel==null)
{
//handle this model being null
}
model.ClientType = enrollModel.ClientType;

Fixing your issue as you have it above though is tough without seeing all code and knowing if there is anything else that may/may not refer to it.



来源:https://stackoverflow.com/questions/12256488/tempdata-not-carrying-over-during-redirecttoaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!