英文:
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字符串参数发送,而不是调用接受模型的方法。
<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("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.
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.
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["InventoryModelObject"] as InventoryModel;
return RedirectToAction("Results", model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Results(InventoryModel model)
{
var list = new List<Source>();
list.AddRange(model.GateWaySelectedSources.Where(x => x.Item2));
list.AddRange(model.BowSelectedSources.Where(x => x.Item2));
list.AddRange(model.CuddySelectedSources.Where(x => x.Item2));
list.AddRange(model.WSSSelectedSources.Where(x => x.Item2));
list.AddRange(model.DeckSelectedSources.Where(x => x.Item2));
list.AddRange(model.ASelectedSources.Where(x => x.Item2));
list.AddRange(model.RSelectedSources.Where(x => x.Item2));
list.AddRange(model.PSelectedSources.Where(x => x.Item2));
var dlnSql = "";
foreach (var item in list)
{
if (string.IsNullOrEmpty(dlnSql))
{
dlnSql = " AND DAMDLN IN (";
}
dlnSql += $"'{item.Item1}', ";
}
if (!string.IsNullOrEmpty(dlnSql))
{
dlnSql += "' ')";
}
var statusSource = model.StatusSelectedSources.Where(x => x.Item2);
var statSql = "";
if (statusSource.Any(x => x.Item1 == "RT"))
{
statSql = " AND DASTAT IN ('RT', ";
}
if (statusSource.Any(x => x.Item1 == "SN"))
{
statSql = string.IsNullOrEmpty(statSql) ? " AND DASTAT IN ('SN', " : statSql + "'SN', ";
}
if (statusSource.Any(x => x.Item1 == "DL"))
{
statSql = string.IsNullOrEmpty(statSql) ? " AND DASTAT IN ('DL', " : statSql + "'DL', ";
}
if (!string.IsNullOrEmpty(statSql))
{
statSql += "' ') ";
}
var yearSql = "";
if (!model.AllYears && model.YearSelectedSources.Any(x => x.Item2))
{
foreach (var item in model.YearSelectedSources)
{
if (item.Item2)
{
yearSql += $"'{item.Item1}', ";
}
}
if (!string.IsNullOrEmpty(yearSql))
{
yearSql = $" AND DAPRGY IN ({yearSql} '0') ";
}
}
var sql = $@"
SELECT DASTAT, DADTRS, TRIM(DASRNO) AS DASRNO, DAMDLN, DAORNO, DAENGD, DAEXTC, DACONF, DACUST, DADTIN
FROM {ViewBag.Library}.DLRINVAPF
WHERE DADLRN={Session["DealershipID"]}{yearSql}{statSql}{dlnSql}";
Session["InventoryModelObject"] = 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.
答案1
得分: 0
在 ASP.NET MVC 中没有“double back”选项。但是你可以使用以下两种方法之一重定向到所需视图:
1) 如果你有一个固定的流程,你可以直接重定向到所需的操作,并提供必要的参数以恢复视图
```csharp
return RedirectToAction("Results", ...);
- 在表单提交后,添加
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't any "double back" 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论