如何在 ASP.NET MVC 中 RedirectToAction 不丢失请求数据 [英] How to RedirectToAction in ASP.NET MVC without losing request data

查看:24
本文介绍了如何在 ASP.NET MVC 中 RedirectToAction 不丢失请求数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ASP.NET MVC 在某些情况下(例如表单提交)可能需要 RedirectToAction.

Using ASP.NET MVC there are situations (such as form submission) that may require a RedirectToAction.

一种这样的情况是,当您在提交表单后遇到验证错误并需要重定向回表单,但希望 URL 反映表单的 URL,而不是它提交到的操作页面.

One such situation is when you encounter validation errors after a form submission and need to redirect back to the form, but would like the URL to reflect the URL of the form, not the action page it submits to.

由于我要求表单包含原始 POST ed 数据,为了用户方便以及验证目的,我如何通过 RedirectToAction()?如果我使用 viewData 参数,我的 POST 参数将更改为 GET 参数.

As I require the form to contain the originally POSTed data, for user convenience, as well as validation purposes, how can I pass the data through the RedirectToAction()? If I use the viewData parameter, my POST parameters will be changed to GET parameters.

推荐答案

解决方案是使用 TempData 属性来存储所需的 Request 组件.

The solution is to use the TempData property to store the desired Request components.

例如:

public ActionResult Send()
{
    TempData["form"] = Request.Form;
    return this.RedirectToAction(a => a.Form());
}

然后在您的表单"操作中,您可以:

Then in your "Form" action you can go:

public ActionResult Form()
{
    /* Declare viewData etc. */

    if (TempData["form"] != null)
    {
        /* Cast TempData["form"] to 
        System.Collections.Specialized.NameValueCollection 
        and use it */
    }

    return View("Form", viewData);
}

这篇关于如何在 ASP.NET MVC 中 RedirectToAction 不丢失请求数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆