【问题标题】:Get Url in post Action of ASP.NET MVC 3在 ASP.NET MVC 3 的 post Action 中获取 Url
【发布时间】:2012-04-17 09:46:57
【问题描述】:

当我提交表单时,我有 2 个索引操作 Post 和 Get,Post Action 返回:

return RedirectToAction("Index");

编辑:

我的get Index Action可以获取一些参数来支持搜索和过滤返回的List。像这样:

public ActionResult Index(string search = "", long searchItem = 0, long RC = 0, long Page = 1)

我使用了分页列表,并且有一个 DropDownList 显示每页中的行数,我编写了一个 jquery 脚本来更改此下拉表单。提交和提交表单时 在RedirectTAction 我丢失了以下代码的参数:

[HttpPost]
public ActionResult Index(FormCollection collection, string search = "") {

    long RowCount = 0;
    long.TryParse(string.Format("{0}", collection["RowCount"]), out RowCount);

//More implement

return RedirectToAction("Index", new { RC = RowCount, search = search, searchItem = Id });

那么有没有办法在 Post Action 中获取 Url 或 url 参数?在这种情况下你有什么建议?

【问题讨论】:

  • 什么url参数?通常当您 POST 时没有查询字符串参数。或者你的情况有吗?您是在谈论查询字符串参数或路由值还是 POST 值?
  • @DarinDimitrov 我更新问题

标签: asp.net-mvc asp.net-mvc-3 url razor http-post


【解决方案1】:

您可以在重定向之前捕获 RouteValueDictionary 中的所有查询字符串参数:

var query = new RouteValueDictionary(
    Request
        .QueryString
        .Keys
        .OfType<string>()
        .ToDictionary(key => key, key => (object)Request.QueryString[key])
);
return RedirectToAction("Index", query);

这只会保留查询字符串参数。如果要添加路由值和 POST 值,可以将它们连接到结果中。

【讨论】:

  • 谢谢,这真的很有帮助,实际上我不想传递发布的所有 Route 值,所以我使用 if(Request.QueryString.AllKeys.Contains("searchItem")) long.TryParse(Request.QueryString.GetValues("searchItem").FirstOrDefault(), out Id);
猜你喜欢
  • 1970-01-01
  • 2011-07-24
  • 2016-11-20
  • 1970-01-01
  • 2012-05-28
  • 2012-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多