Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.BeginForm with route id parameter and get parameter on submit?

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?

like image 287
Phoenix_uy Avatar asked Mar 13 '14 14:03

Phoenix_uy


1 Answers

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))
like image 50
Cloud SME Avatar answered Oct 19 '22 22:10

Cloud SME