简体   繁体   English

当我尝试ReturnRedirect时,TempData被覆盖

[英]TempData gets overrided when I try to ReturnRedirect

I want to redirect to a previous page after I submit some data in database. 在数据库中提交一些数据后,我想重定向到上一页。 I am using a TempData to retrieve ID on current Page, then I am going on the page with the form I want to submit ( which have an ID ), but after the POST method is fired TempData gets overrided by current page ID which is the page with the form and I want to ReturnRedirect to initial page with its ID. 我正在使用TempData来检索当前页面上的ID ,然后使用要提交的表单( which have an ID )进入current page ID ,但是在触发POST方法后, TempDatacurrent page ID覆盖,即表单的页面,我想用其ID返回ReturnRedirect到初始页面。

Here the code: 这里的代码:

Controller with GET method where I retrieve the ID: 我使用GET方法检索ID的控制器:

var currentID = Url.RequestContext.RouteData.Values["id"];          
TempData["currentId"] = currentID;

Controller with POST method where I try to redirect: 我尝试重定向的带有POST方法的控制器:

if (ModelState.IsValid)
{
    // Editing records in database
    ....
    return RedirectToAction("Details", "Jobs", 
        new { controller = "JobsController", action = "Details", id = TempData["currentId"] });
}
ModelState.AddModelError("", "Something failed");
return View();

I am using this approach because its working if the current action with a POST method doesn't have an ID. 我之所以使用这种方法,是因为如果使用POST方法的当前操作没有ID,则可以使用该方法。

Thank you for any suggestions. 感谢您的任何建议。

EDIT: 编辑:

I have a Details of Jobs: 我有一份工作的详细信息:

    // GET: Jobs/Details/5
    public ActionResult Details(Guid id)
    {
        var currentID = Url.RequestContext.RouteData.Values["id"];          
        TempData["currentId"] = currentID;

        var currentUserTemp = LoggedUserId;
        TempData["userID"] = currentUserTemp;

        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Job job = db.Jobs.Find(id);
        if (job == null)
        {
            return HttpNotFound();
        }

        return View(job);
    }

But from this page I want a link to a page where I edit current user data. 但是从此页面我想要一个指向我在其中编辑当前用户数据的页面的链接。

View Details page Code: 查看详细信息页面代码:

@Html.ActionLink("Edit Company Details", "Edit", "UserAdmin", new { id = TempData["userID"] }, null)

Now I am on edit User Page, I edit the form and use save button (which triggers POST method) I want to use the other TempData variable to ReturnRedirect on Details Page but since both pages have ID`s, TempData gets overrided with last ID from the URL which is the ID of the user not ID. 现在,我在编辑用户页面上,我编辑表单并使用保存按钮(触发POST方法),我想使用其他TempData变量在“详细信息”页面上返回ReturnRedirect ,但是由于两个页面都有ID,因此TempData被最后一个ID覆盖从URL(这是用户的ID)而不是ID。

Don't forget that TempData exists only during the time of a HTTP Request. 不要忘记TempData仅在HTTP请求期间存在。 Maybe it's a clue to your problem (I can't find all the interactions between Controllers and Views in your code so I can't be sure). 也许这是您问题的线索(我无法在您的代码中找到Controller和View之间的所有交互,所以我不确定)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试写入列表时,那里的先前文本被覆盖 - When I try to write to a List my Previous text in there gets Overwritten 被覆盖的方法不会被调用 - overrided method doesn't gets called 当我尝试处理轮询器时,NetMQ 卡住了(请求-回复模式) - NetMQ gets stuck when I try to dispose the Poller (Request-Reply pattern) 当我在最后 30 秒内尝试更改颜色时,我的计时器卡住了 - My timer gets jammed when I try to change colour in the last 30 seconds 当我尝试通过 HttpWebRequest 上传音频文件时,Hololens 应用程序卡住了 - Hololens app gets stuck when I try to upload an audio file via HttpWebRequest 当我尝试在另一个类方法中检索信息时,缓存将为空 - Cache gets null when I try to retrieve information in another class method 使用TempData时的电子邮件问题 - Emails issues when using TempData 为什么tempData在tempData中添加很多项时总是为null? - why tempData always null when add many item in tempData? 浏览器刷新时如何保留 TempData 但如果用户离开页面,我如何保留 null - How can I keep TempData when browser refresh but null if user leave the page 使用MVC3,当用户单击HTML.ActionLink控件时,如何设置TempData值? - Using MVC3, how do I set a TempData value when the user clicks on an HTML.ActionLink control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM