如何重定向POST请求的URL保持MVC模型值 [英] How to redirect a POST request to a url maintaining model values in MVC
问题描述
我有一个相当标准的排序/过滤器/搜索页的形式,但需要对网址的格式控制。排序/过滤/页的参数都应该是URL的一部分,这样,例如,地址可以通过电子邮件发送给别人。
在添加了另一个筛选参数后,一个POST请求。我的控制器方法是这样的:
[HttpPost]
公众的ActionResult搜索(字符串filterField,
操作filterOperator,
串filterValue,
PeopleGroupSearchModel模型);
的 PeopleGroupSearchModel
从查询字符串参数填充。在过滤器*
参数从提交的表单值来了。
我想分析所提供的过滤器值,这将随后在名为过滤器模式
过滤器添加到集合。然后,采取更新的模型,并将其转换为相应的URL,并通过,作为响应于用户
因此,举例来说,如果他们在此页面上:
<?pre> PeopleGroup /搜索页= 4和排序=国家
...和POST:
- filterField = PeopleGroupName
- filterOperator =等于
- filterValue =祖鲁
......一旦所有的处理完成后,在其浏览器的地址应该是这样的:
<$p$p><$c$c>PeopleGroup/Search?page=4&sort=Country&PeopleGroupName=Zulu&PeopleGroupName_op=Equals所以,或多或少什么,我试图做的:
[HTTPGET]
公众的ActionResult搜索(PeopleGroupSearchModel模型)
{
PeopleGroupData.Search(模型);
ViewData.Model =模型;
返回查看();
}[HttpPost]
公众的ActionResult搜索(字符串filterField,
操作filterOperator,
串filterValue,
PeopleGroupSearchModel模型)
{
PeopleGroupFilter过滤= ParseFilter(filterField,
filterOperator,
filterValue);
model.Filters.Add(过滤器);
返回RedirectToAction(搜索,???);
}
我的非常的新MVC,所以如果我要对这个完全错误的方式,请让我知道!
这可能是违反MVC坚持的原则,但一旦我停止战斗框架,只是想到什么,我试图在HTTP的土地做,这个简单解决方案对我的作品:
[HttpPost]
公众的ActionResult搜索(PeopleGroupColumn filterField,
操作filterOperator,
串filterValue)
{
VAR收集=
HttpUtility.ParseQueryString(Request.QueryString.ToString());
collection.Set(filterField.ToString(),filterValue);
collection.Set(filterField.ToString()+_OP,filterOperator.ToString());
返回新RedirectResult(
Request.Url.AbsolutePath +? + collection.ToString());
}
I have a fairly standard sort/filter/page search form but need control over the format of the url. The sort/filter/page parameters should all be part of the url so that, for example, the address could be emailed to someone.
When another filter parameter is added, a POST request is made. My controller method looks like this:
[HttpPost]
public ActionResult Search(string filterField,
Operator filterOperator,
string filterValue,
PeopleGroupSearchModel model);
The PeopleGroupSearchModel
is populated from query string parameters. The filter*
parameters are coming from the posted form values.
I would like to parse the provided filter values, which will then add a filter to a collection in the model called Filters
. Then, take the updated model and convert it to the appropriate url and pass that as the response to the user.
So, for example, if they are on this page:
PeopleGroup/Search?page=4&sort=Country
... and POST:
- filterField = PeopleGroupName
- filterOperator = Equals
- filterValue = Zulu
... once all the processing is done, the address in their browser should be something like:
PeopleGroup/Search?page=4&sort=Country&PeopleGroupName=Zulu&PeopleGroupName_op=Equals
So, more or less what I am trying to do:
[HttpGet]
public ActionResult Search(PeopleGroupSearchModel model)
{
PeopleGroupData.Search(model);
ViewData.Model = model;
return View();
}
[HttpPost]
public ActionResult Search(string filterField,
Operator filterOperator,
string filterValue,
PeopleGroupSearchModel model)
{
PeopleGroupFilter filter = ParseFilter(filterField,
filterOperator,
filterValue);
model.Filters.Add(filter);
return RedirectToAction("Search", ???);
}
I am very new to MVC, so if I am going about this completely the wrong way, please let me know!
This is probably a violation of MVC priciples, but once I stopped fighting the framework and just thought about what I was trying to do in HTTP-land, this simple solution works for me:
[HttpPost]
public ActionResult Search(PeopleGroupColumn filterField,
Operator filterOperator,
string filterValue)
{
var collection =
HttpUtility.ParseQueryString(Request.QueryString.ToString());
collection.Set(filterField.ToString(), filterValue);
collection.Set(filterField.ToString() + "_op", filterOperator.ToString());
return new RedirectResult(
Request.Url.AbsolutePath + "?" + collection.ToString());
}
这篇关于如何重定向POST请求的URL保持MVC模型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!