0

I have a form which on submit passes all input field values as a querystring.

The address i get after submitting is:

http://localhost:51246/Markets?Keyword=keywordhere

The preferred address format is:

http://localhost:51246/Markets/keywordhere

The code for the form:

  @using (Html.BeginForm("Index", "Markets", FormMethod.Get, new { id = "login", role = "form", @dataToggle = "validator", @class = "form-horizontal" }))
    {

        <div class="form-group">
            @Html.LabelFor(x => x.Keyword, new { @class = "col-sm-2 control-label" })
            <div class="col-sm-10">
                @Html.TextBoxFor(x => x.Keyword, new { @class = "form-control", @placeholder = "Enter market or country name..." })
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-primary">Filter</button>
                <a href="@Url.Action("Index")" class="btn btn-default">Reset</a>
            </div>
        </div>
    }

My route config is:

   routes.MapRoute(
       "Markets",
       "{controller}/{keyword}/{page}",
       new
       {
           controller = "Markets",
           action = "Index",
           keyword = UrlParameter.Optional,
           page = UrlParameter.Optional
       }
       );

        routes.MapRoute(
          "Markets main",
          "{controller}/{action}/{id}",
          new
          {
              controller = "Markets",
              action = "Index",
              id = UrlParameter.Optional,
              }
              );

...

And the controller is:

[HttpGet]
public async Task<ActionResult> Index(string keyword, int page = 1)
{
        //search logic and create viewmodel
        Return ActionResult(viewmodel)
}

The functionality works fine, and if i type the preferred format into the address bar and hit enter, the expected search results are displayed.

How can i make the form submit and place the values in the preferred format?

Sam Jones
  • 4,443
  • 2
  • 40
  • 45
  • This answer doesn't actually answer my question. If it was just the id then it's not an issue, it the additional values. Does this mean that asp.net mvc forms don't use asp.net route values? – Sam Jones Jun 21 '16 at 09:19
  • It does answer your question. You cannot get `/Markets/keywordhere` because the browser knows nothing about your routes (as explained in the dupe) and a `FormMethod.Get` will always send the values of its form controls as query string parameters - in your case the value for `Keyword` (as explained in the dupe). And the only way to get your preferred format is to use javascript to build a url based on the form control values and redirect (as explained in the dupe) –  Jun 22 '16 at 00:08
  • @StephenMuecke Ok thanks Stephen, i guess i was just hoping for an mvc routeconfig/controller route attribute/mvc feature to handle this, i'll probably end up building the url via javascript in the end... – Sam Jones Jun 22 '16 at 09:40
  • Nothing in MVC can do that - its all happening on the client and the client knows nothing about the code on the server :) –  Jun 22 '16 at 09:44

0 Answers0