0

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:

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:

How do I redirect to the previous action in ASP.NET MVC?

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.

screenshot

podrick
  • 35
  • 6

1 Answers1

0

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", ...);
  1. 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adalyat Nazirov
  • 1,611
  • 2
  • 14
  • 28
  • I am getting `The resource cannot be found.` If it helps, `Results` takes a Model parameter, which I have code to fill if it is null. – podrick Jun 05 '23 at 14:49
  • If you have a model with multiple parameters then Redirect to action is your way – Adalyat Nazirov Jun 05 '23 at 14:55
  • I tried passing the model, but it converted it into a json string and used it as a parameter (see updated question) – podrick Jun 05 '23 at 15:34
  • When you RedirectToAction you have to pass route Params, but not the model itself. After the redirect the target action should build/fetch model based on route parameters and call `return View(model);` – Adalyat Nazirov Jun 05 '23 at 15:43
  • Can you show your results action method too? – Adalyat Nazirov Jun 05 '23 at 15:44
  • Updated question to show `ActionResult Results(InventoryModel model)` – podrick Jun 05 '23 at 15:54
  • Oh! Results annotated as Post only action method. Do you really need that? in that can you can either support Get too or make a call from you javascript. – Adalyat Nazirov Jun 05 '23 at 16:00
  • Sadly, yes. The original `Query` is a **POST** to the `Results`. Maybe it can't be done with ASP.NET MVC – podrick Jun 05 '23 at 17:00
  • In that case you can firstly redirect to details or any dummy page, and after that make a Ajax/Form Post request to Results. – Adalyat Nazirov Jun 05 '23 at 17:05
  • Please investigate this approach https://stackoverflow.com/q/133925/2091519 it will allow you to either generate form with require parameters or populate existing and submit when page load or timeout. So you will be auto redirected from Details to Results – Adalyat Nazirov Jun 05 '23 at 17:12