ASP.NET MVC:在表单提交后,我如何重定向到先前的操作或特定操作?

huangapple go评论52阅读模式
英文:

ASP.NET MVC: How can I redirect to a previous action or a specific action after form submission?

问题

I will provide the translation for the code sections you provided:

My pages have the form:

  - `site.com/inventory/Query`
  - `site.com/inventory/Results`
  - `site.com/inventory/Details?item=123`

在我的页面中,有以下表单:

  - `site.com/inventory/Query`
  - `site.com/inventory/Results`
  - `site.com/inventory/Details?item=123`

View
---

On `Details`, I have a button to upload the item data sheet in the View:

在“Details”页面上,我有一个用于上传项目数据表的按钮:

```c#
using (Html.BeginForm("Upload", "Inventory", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.TextBox("file", "", new { type = "file", id = "file", accept = "Application/PDF" })
    @Html.HiddenFor(m => m.ItemNumber, "item")
    <button type="submit" class="btn btn-primary">Upload</button>
}

The initial Query is a lot of work on the database (it takes about 20 seconds). Instead of returning to it, I would like to get back to the Results page after uploading the support document for this item on the Detail page.

最初的“Query”在数据库中需要很多工作(大约需要20秒)。我想在上传支持文档后,而不是返回到它,返回到“Results”页面。

I found a similar question:

我找到了一个类似的问题:

https://stackoverflow.com/q/815229/22019322

I modified my code to include this Redirect:

我修改了我的代码以包括这个重定向

Controller

[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult Upload(HttpPostedFileBase file, string item)
{
    // saves the file (code hidden)
    return Redirect($"{Request.UrlReferrer}");
}

However, the Redirect above successfully returns me to the Details page.

然而,上面的重定向成功地将我返回到了“Details”页面。

Is there a way to redirect all the way back to the Results page?

有没有办法将重定向直接返回到“Results”页面?

Update

I have tried updating to this, based on the answer below:

我已经尝试根据下面的答案进行更新:

[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult Upload(HttpPostedFileBase file, string item)
{
    // saves the file (code hidden)
    var model = Session["InventoryModelObject"] as InventoryModel;
    return RedirectToAction("Results", model);
}

[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult Results(InventoryModel model)
{
    // Code for processing the model and returning the Results page
    // ...
    return View(result);
}

But this is trying to send my model as a JSON string parameter instead of calling the method that accepts the model.

但这似乎试图将我的模型作为JSON字符串参数发送,而不是调用接受模型的方法。

screenshot

screenshot


<details>
<summary>英文:</summary>

My pages have the form:

  - `site.com/inventory/Query`
  - `site.com/inventory/Results`
  - `site.com/inventory/Details?item=123`


View
---

On `Details`, I have a button to upload the item data sheet in the View:

```c#
using (Html.BeginForm(&quot;Upload&quot;, &quot;Inventory&quot;, FormMethod.Post, new { enctype = &quot;multipart/form-data&quot; }))
{
    @Html.AntiForgeryToken()
    @Html.TextBox(&quot;file&quot;, &quot;&quot;, new { type = &quot;file&quot;, id = &quot;file&quot;, accept = &quot;Application/PDF&quot; })
    @Html.HiddenFor(m =&gt; m.ItemNumber, &quot;item&quot;)
    &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Upload&lt;/button&gt;
}

The initial Query is a lot of work on the database (it takes about 20 seconds). Instead of returning to it, I would like to get back to the Results page after uploading the support document for this item on the Detail page.

I found a similar question:

https://stackoverflow.com/q/815229/22019322

I modified my code to include this Redirect:

Controller

[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult Upload(HttpPostedFileBase file, string item)
{
    // saves the file (code hidden)
    return Redirect($&quot;{Request.UrlReferrer}&quot;);
}

However, the Redirect above successfully returns me to the Details page.

Is there a way to redirect all the way back to the Results page?

Update

I have tried updating to this, based on the answer below:

[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult Upload(HttpPostedFileBase file, string item)
{
    // saves the file (code hidden)
    var model = Session[&quot;InventoryModelObject&quot;] as InventoryModel;
    return RedirectToAction(&quot;Results&quot;, model);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Results(InventoryModel model)
{
    var list = new List&lt;Source&gt;();
    list.AddRange(model.GateWaySelectedSources.Where(x =&gt; x.Item2));
    list.AddRange(model.BowSelectedSources.Where(x =&gt; x.Item2));
    list.AddRange(model.CuddySelectedSources.Where(x =&gt; x.Item2));
    list.AddRange(model.WSSSelectedSources.Where(x =&gt; x.Item2));
    list.AddRange(model.DeckSelectedSources.Where(x =&gt; x.Item2));
    list.AddRange(model.ASelectedSources.Where(x =&gt; x.Item2));
    list.AddRange(model.RSelectedSources.Where(x =&gt; x.Item2));
    list.AddRange(model.PSelectedSources.Where(x =&gt; x.Item2));
    var dlnSql = &quot;&quot;;
    foreach (var item in list)
    {
        if (string.IsNullOrEmpty(dlnSql))
        {
            dlnSql = &quot; AND DAMDLN IN (&quot;;
        }
        dlnSql += $&quot;&#39;{item.Item1}&#39;, &quot;;
    }
    if (!string.IsNullOrEmpty(dlnSql))
    {
        dlnSql += &quot;&#39; &#39;)&quot;;
    }
    var statusSource = model.StatusSelectedSources.Where(x =&gt; x.Item2);
    var statSql = &quot;&quot;;
    if (statusSource.Any(x =&gt; x.Item1 == &quot;RT&quot;))
    {
        statSql = &quot; AND DASTAT IN (&#39;RT&#39;, &quot;;
    }
    if (statusSource.Any(x =&gt; x.Item1 == &quot;SN&quot;))
    {
        statSql = string.IsNullOrEmpty(statSql) ? &quot; AND DASTAT IN (&#39;SN&#39;, &quot; : statSql + &quot;&#39;SN&#39;, &quot;;
    }
    if (statusSource.Any(x =&gt; x.Item1 == &quot;DL&quot;))
    {
        statSql = string.IsNullOrEmpty(statSql) ? &quot; AND DASTAT IN (&#39;DL&#39;, &quot; : statSql + &quot;&#39;DL&#39;, &quot;;
    }
    if (!string.IsNullOrEmpty(statSql))
    {
        statSql += &quot;&#39; &#39;) &quot;;
    }
    var yearSql = &quot;&quot;;
    if (!model.AllYears &amp;&amp; model.YearSelectedSources.Any(x =&gt; x.Item2))
    {
        foreach (var item in model.YearSelectedSources)
        {
            if (item.Item2)
            {
                yearSql += $&quot;&#39;{item.Item1}&#39;, &quot;;
            }
        }
        if (!string.IsNullOrEmpty(yearSql))
        {
            yearSql = $&quot; AND DAPRGY IN ({yearSql} &#39;0&#39;) &quot;;
        }
    }
    var sql = $@&quot;
SELECT DASTAT, DADTRS, TRIM(DASRNO) AS DASRNO, DAMDLN, DAORNO, DAENGD, DAEXTC, DACONF, DACUST, DADTIN
FROM {ViewBag.Library}.DLRINVAPF
WHERE DADLRN={Session[&quot;DealershipID&quot;]}{yearSql}{statSql}{dlnSql}&quot;;
    Session[&quot;InventoryModelObject&quot;] = model;
    return View(result);
}

But this is trying to send my model as a JSON string parameter instead of calling the method that accepts the model.

screenshot

答案1

得分: 0

在 ASP.NET MVC 中没有“double back”选项。但是你可以使用以下两种方法之一重定向到所需视图:

1) 如果你有一个固定的流程,你可以直接重定向到所需的操作,并提供必要的参数以恢复视图

```csharp
return RedirectToAction("Results", ...);
  1. 在表单提交后,添加 returnUrl 查询或提交表单参数以进行重定向。例如:
using (Html.BeginForm("Upload", "Inventory", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.TextBox("file", "", new { type = "file", id = "file", accept = "Application/PDF" })
    <input type="hidden" name="returnUrl" value="@Request.UrlReferrer" />
    @Html.HiddenFor(m => m.ItemNumber, "item")
    <button type="submit" class="btn btn-primary">Upload</button>
}

或者你可以将它放入 URL 查询字符串中。


<details>
<summary>英文:</summary>
There isn&#39;t any &quot;double back&quot; in ASP.NET MVC. But you could redirect to the desired view using one of two approaches
1) In case you have a permanent flow you could just redirect to desired action providing the required parameter to restore view

return RedirectToAction("Results", ...);


2) Add `returnUrl` query or post form parameter to redirect to after form submitted. For example:

using (Html.BeginForm("Upload", "Inventory", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.TextBox("file", "", new { type = "file", id = "file", accept = "Application/PDF" })
<input type="hidden" name="returnUrl" value="@Request.UrlReferrer" />
@Html.HiddenFor(m => m.ItemNumber, "item")
<button type="submit" class="btn btn-primary">Upload</button>
}


Or you could put that into the URL query string.
</details>

huangapple
  • 本文由 发表于 2023年6月5日 03:12:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定