I have a method like this in my mvc project:
[Route("Categories/{categoryId}")]
public ActionResult List(SearchQueryCommand searchQuery) {
//Stuff
return View();
}
So this method is called like: 'www.myweb.com/Categories/212'
But i have a dropdownlist with a form that sorts this query and send it back again to the server to get the sorted results. I have this in my razor view:
@using (Html.BeginForm(new { categoryId = Model.CategoryId }))
{
<div>
<small>@Html.LabelFor(m => m.Order, "sort by")</small>
@Html.DropDownListFor(m => m.Order, Model.ProductSortByOptions, new { onchange = "this.form.submit();" })
<button class="nojs" type="submit">ok</button>
</div>
}
My problem is that i dont know how to format my code to have in my url something like: 'www.myweb.com/Categories/213?Order=MinPrice'
.
What are my options to perform this?
The method of your form needs to be a GET
:
@using (Html.BeginForm(new { categoryId = Model.CategoryId }, FormMethod.Get))
This way, when the form is submitted, all of the form fields will be added to the querystring in the URL.
To post to specific action method, you will need to specify them in the BeginForm
definition. I'm not too familiar with attribute-based route definition but one of these should work:
@using (Html.BeginForm("List", "Categories", new { categoryId = Model.CategoryId }, FormMethod.Get))
Or
@using (Html.BeginForm("Index", "Categories", new { categoryId = Model.CategoryId }, FormMethod.Get))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With