In my productcontroller I have two actionresult returning methods:
[Route("Shop/{brand}/{category}/{subcategory?}/{page:int?}")]
public ActionResult Index(string brand, string category, string subcategory, int? page, SortOptions currentSort = SortOptions.SinceDesc)
{ //...
and
[HttpPost]
[Route("Shop/{brand}/{category}/{subcategory?}/{page:int?}")]
public ActionResult Index(ProductsViewModel pvm)
{ //...
And this is my razor view:
@using (@Html.BeginForm("Index", "Products", FormMethod.Post))
{
@Html.DropDownListFor(x => x.SubCatID, Model.SubCategoriesSelectList, new { @class = "multiselect" })
}
When I submit the page it hits the httppost method but the url is still: Shop/nike/shoes even when I selected the subcategory runningshoes from the dropdown. I would like to have urls like:
Being more of a webform-guy I am having a hard time to navigate to new url's and using viewmodel properties as parameters.
edit posted my UI:
to explain my ui:
the first dropdown should get
to a subcategory. for instance: shop/nike/shoes/runningshoes
The second should post 'back' to sort the products.
The price slider should post back because it should filter. (would filter client side if there was no paging)
The paging should get so you can deeplink to a certain page: shop/nike/shoes/runningshoes/page2 etc.
In your BeginForm(...)
you end up having to pass a route values dictionary with Subcategory = "runningshoes"
This does mix values being passed by GET, aka in the querystring through the route values dictionary, and POST, which will be the values from the form, but will accomplish what you are trying to do. More can be read about the BeginForm(..)
overload Here on MSDN
You should end up with:
@using (@Html.BeginForm("Index", "Products", new { subcategory = "runningshoes" }, FormMethod.Post))
{
@Html.DropDownListFor(x => x.SubCatID, Model.SubCategoriesSelectList, new { @class = "multiselect" })
}
EDIT
Just realized you want the value from the form post to be in the QueryString on the response. Instead of returning the view directly from your MVC method for the Form Post what you could possibly do is a return RedirectToAction("ActionName", "ControllerName", new { subcategory = request.SubCategory });
which you would have an action that would support this redirect specifically.
Additional information on redirect to action can be found here on MSDN