Shoter Shoter - 1 month ago 9
C# Question

BeginForm() take complex input value into routing

I have a little problem with binding

<input>
value into routing of the beginForm GET action when submitting.

Firstly, the relevant code :

[Route("Company/{companyID:int}/MarketOffers/{PagingParam.PageNumber=1}")]
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 :

http://localhost:38120/Company/2037/MarketOffers/3


* 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 :

http://localhost:38120/Company/2037/MarketOffers?PagingParam.PageNumber=2


Instead of

http://localhost:38120/Company/2037/MarketOffers/2


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

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