在c#mvc中重定向页面

时间:2013-11-12 02:46:32

标签: asp.net-mvc asp.net-mvc-4

我有链接

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

点击“删除”按钮后,页面应重定向到“索引”

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

但我想保持链接与第一次相同,我不知道有什么方法,请帮助我。感谢

我可以解决它,我在

中保存会话
public ActionResult Index{
  Session["LastPage"] = Request.Url.ToString();
}

在我

之后

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

3 个答案:

答案 0 :(得分:2)

您可以将查询字符串传递给RedirecToAction

的第三个参数
return RedirectToAction("Index","PaymentOrder", new { fromDate = model.FromDate });

或者传递整个模型,其中包含与查询字符串类似的属性

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

答案 1 :(得分:2)

由于您的查询字符串很长,因此编写扩展方法并使用它来保持控制器精简可能会更好。我没有测试过这个,但是这样的事情应该有效:

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;
}

然后在你的控制器中:

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

答案 2 :(得分:0)

只是不重定向但返回视图,URL将保持不变。