[英]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
方法后, TempData
被current 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.