user2888692
user2888692

Reputation: 101

Redirect page in c# mvc

I have link

http://localhost:3163/PaymentOrder?AgentCode=&InvoiceNo=&AgentName=&FromDate=&fromDate=12%2F11%2F2013&FromDate=12%2F11%2F2013+9%3A08%3A01+SA&toDate=12%2F11%2F2013

after click button "Delete" the page should be redirect to "Index"

        return RedirectToAction("Index","PaymentOrder");

But i want keep link same as first, i don't know what method, please help me. thanks

I can fix it, i save session in

public ActionResult Index{
  Session["LastPage"] = Request.Url.ToString();
}

after I'm

return Redirect(Session["LastPage"] as String);

Upvotes: 4

Views: 2317

Answers (3)

CodeCaster
CodeCaster

Reputation: 151720

Just don't redirect but return the view, the URL will remain the same.

Upvotes: 1

John H
John H

Reputation: 14640

As your query string is quite long, it would probably be better to write an extension method and use that instead, to keep your controllers thin. I haven't tested this, but something like this should work:

public static RouteValueDictionary ToRouteDictionary(this NameValueCollection nameValues)
{
    if (nameValues == null || nameValues.HasKeys() == false)
        return new RouteValueDictionary();

    var routeValues = new RouteValueDictionary();

    foreach (var key in nameValues.AllKeys)
        routeValues.Add(key, nameValues[key]);

    return routeValues;
}

Then in your controller:

return RedirectToAction("Index","PaymentOrder", Request.QueryString.ToRouteDictionary());

Upvotes: 2

WannaCSharp
WannaCSharp

Reputation: 1898

You can pass the query strings to the third parameter of RedirecToAction

return RedirectToAction("Index","PaymentOrder", new { fromDate = model.FromDate });

Or pass the entire model as well, that contains the properties similar to your query strings

return RedirectToAction("Index","PaymentOrder", new { paymentModel = model });

Upvotes: 2

Related Questions