Shoter Shoter - 3 years ago 94
C# Question

BeginForm() take complex input value into routing

I have a little problem with binding

value into routing of the beginForm GET action when submitting.

Firstly, the relevant code :

public ActionResult MarketOffers(int companyID, PagingParam pagingParam)

* This is declaration of action that I am dealing with. PagingParam have property PageNumber of int type.

@using (Html.BeginForm(RouteDataHelper.ActionName, RouteDataHelper.ControllerName, FormMethod.Get, new { companyID = Model.Info.CompanyID }))

* In the BeginForm I am only using companyID because PageNumber will vary depending on what the user clicks on the website and because of that it's not supplied here.

<input id="PagingParam.PageNumber" name="PagingParam.PageNumber" type="hidden" value="1">

* This is input which value should be appended to URL. It's inside the form which generation code you see above. The value changes by javascript just before submitting the form.

When I input link like :


* Everything is ok. PagingParam.PageNumber sets its value to 3. This is desired behaviour.

But when I submit the form with PagingParam.PageNumber input field then my URL looks like :


Instead of


I exactly know that I could deal with that and change form action value using javascript+JQuery but I have a feeling that there is a way to do this automatically in MVC without writing JavaScript.

So the question is : Can I do something to automatically generate desired link when I am passing form which action is (for example) "/Company/2037/MarketOffers" and append PagingParam.PageNumber to generated URL without writing JavaScript?

Answer Source

All inputs inside a form that make a GET have their values added as query string parameters because a browser has no knowledge of the routes on your server.

Its not possible to generate ../Company/2037/MarketOffers/3 without using javascript to modify the action attribute of the form

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download