当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc - 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);

网友答案:

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 });
网友答案:

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());
网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: