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!