将 POST 中的 Dictionary<string,string> 传递给 GET 方法(作为 url 参数)ASP.NET MVC
Pass Dictionary<string,string> from POST to GET method (as url parameters) ASP.NET MVC
我有问题。我想通过 RouteValueDictionary 通过 RedirectToAction 传递字典。有没有可能做到这一点?
我有一个POST方法:
[HttpPost]
public ActionResult Search(MyViewModel _myViewModel)
{
IDictionary<string, string> parameters = new Dictionary<string, string>();
foreach (var item in _myViewModel)
{
parameters.Add(item.ValueId, item.ValueName);
}
return RedirectToAction("Search", new RouteValueDictionary(parameters));
}
我想要一个这样的 url:
http://localhost:26755/Searcher/Search?id1=value1&id2=value2&id3=value3
GET 方法应该是什么样子?
[HttpGet]
public ActionResult Search( **what's here?** )
{
(...)
return View(myViewModel);
}
首先,我们需要修复执行重定向的 Search
操作。如果您想在重定向时获得所需的查询字符串参数,则应使用 IDictionary<string, object>
而不是 IDictionary<string, string>
:
[HttpPost]
public ActionResult Search(MyViewModel _myViewModel)
{
IDictionary<string, object> parameters = new Dictionary<string, object>();
foreach (var item in _myViewModel)
{
parameters.Add(item.ValueId, item.ValueName);
}
return RedirectToAction("Search", new RouteValueDictionary(parameters));
}
并且在目标控制器操作中完成此操作后,您可以在请求中使用 QueryString
字典:
[HttpGet]
public ActionResult Search()
{
// this.Request.QueryString is the dictionary you could use to access the
// different keys and values being passed
// For example:
string value1 = this.Request.QueryString["id1"];
...
// or you could loop through them depending on what exactly you are trying to achieve:
foreach (string key in this.Request.QueryString.Keys)
{
string value = this.Request.QueryString[key];
// do something with the value here
}
...
}
我有问题。我想通过 RouteValueDictionary 通过 RedirectToAction 传递字典。有没有可能做到这一点?
我有一个POST方法:
[HttpPost]
public ActionResult Search(MyViewModel _myViewModel)
{
IDictionary<string, string> parameters = new Dictionary<string, string>();
foreach (var item in _myViewModel)
{
parameters.Add(item.ValueId, item.ValueName);
}
return RedirectToAction("Search", new RouteValueDictionary(parameters));
}
我想要一个这样的 url:
http://localhost:26755/Searcher/Search?id1=value1&id2=value2&id3=value3
GET 方法应该是什么样子?
[HttpGet]
public ActionResult Search( **what's here?** )
{
(...)
return View(myViewModel);
}
首先,我们需要修复执行重定向的 Search
操作。如果您想在重定向时获得所需的查询字符串参数,则应使用 IDictionary<string, object>
而不是 IDictionary<string, string>
:
[HttpPost]
public ActionResult Search(MyViewModel _myViewModel)
{
IDictionary<string, object> parameters = new Dictionary<string, object>();
foreach (var item in _myViewModel)
{
parameters.Add(item.ValueId, item.ValueName);
}
return RedirectToAction("Search", new RouteValueDictionary(parameters));
}
并且在目标控制器操作中完成此操作后,您可以在请求中使用 QueryString
字典:
[HttpGet]
public ActionResult Search()
{
// this.Request.QueryString is the dictionary you could use to access the
// different keys and values being passed
// For example:
string value1 = this.Request.QueryString["id1"];
...
// or you could loop through them depending on what exactly you are trying to achieve:
foreach (string key in this.Request.QueryString.Keys)
{
string value = this.Request.QueryString[key];
// do something with the value here
}
...
}