0

I have a search box in a Razor template:

    @{
        using (Html.BeginForm("Detail", "Book", FormMethod.Get))
        {
        @Html.TextBox("Id")
        <input type="submit" value="Search" />
        }
    }

When I submit a search it goes to a url like:

~/Book/Detail?Id=1234

However I want it to format the url like so, just because I think it looks cleaner:

~/Book/Detail/1234

Which works perfectly fine because the controller method signature looks like this:

    // GET: /Book/Detail/id
    public ActionResult Detail(string id)

Model with TextBoxFor

I've tried a Html.TextBoxFor:

    @model WebApplication.Models.SearchModel
    @{
        using (Html.BeginForm("Detail", "Book", FormMethod.Get))
        {
        @Html.TextBoxFor(m => m.Id)
        <input type="submit" value="Search" />
        }
    }

Same result.

weston
  • 54,145
  • 21
  • 145
  • 203

4 Answers4

1

I think you want to take a look at the @Html.BeginRouteForm method, like in this question.

Community
  • 1
  • 1
Stephen
  • 2,027
  • 2
  • 22
  • 26
  • Thanks, I'm giving it a go, but it's got a load of overloads and not much help on MSDN. – weston Aug 07 '13 at 12:47
  • meh... isn't really an option on second look. Sorry about that. Trouble is that for the 'route-like' syntax, the id needs to be passed into the BeginForm method. See the question and answer here: http://stackoverflow.com/questions/13997629/how-to-get-razor-form-to-use-route – Stephen Aug 07 '13 at 13:56
  • No problem, thanks for the help, I've found a [solution](http://stackoverflow.com/a/18105353/360211) – weston Aug 07 '13 at 14:11
1

You use a GET request. This means that all parameters will appear in the url box. I can't check now, but I suppose you could use these options:

  1. The IIS url rewrite - http://www.iis.net/downloads/microsoft/url-rewrite
  2. Url rewrite through a web.config - http://www.hanselman.com/blog/RedirectingASPNETLegacyURLsToExtensionlessWithTheIISRewriteModule.aspx

And a batch of stupid methods:

  1. You can change your request to POST and then modificate the Url by the JS - Modify the URL without reloading the page
  2. You can redirect the request

Also, did you try to add a personal routing for the search url?

Community
  • 1
  • 1
Mark Twain
  • 826
  • 14
  • 26
  • OK, well I'll bare those in mind, but I'm sure there is a simple way. – weston Aug 07 '13 at 13:01
  • As for the personal routing - no I didn't try that, I don't know how or why? – weston Aug 07 '13 at 13:02
  • How: in the `Global.asax` in the method `RegisterRoutes`. Why: I suppose that you shouldn't do this, because your Controller/Action/Parameter are same as default routing. I just didn't notice it at first time. – Mark Twain Aug 07 '13 at 13:42
  • Yeah both urls work, so the mapping is there, I think now that it boils down to the html `` submit will always put parameters at end of url after a `?`. So maybe the other ideas you put forward are nessasary. – weston Aug 07 '13 at 13:47
  • 1
    Thanks for your suggestions, I found a simple solution, see [answer](http://stackoverflow.com/a/18105353/360211). – weston Aug 07 '13 at 13:55
0

Try using a model for the form submit and use @Html.TextBoxFor.

Miller
  • 1,096
  • 9
  • 22
0

The answer was to add a new search action then redirect to the detail. This is nice because I can choose to do more when searching, such as returning a different view if the query has multiple matches.

    //
    // GET: /Book/Search?query=
    public ActionResult Search(string query)
    {
        return RedirectToAction("Detail", new { id = query });
    }

    //
    // GET: /Book/Detail/id
    public ActionResult Detail(string id)

Razor:

@{
    using (Html.BeginForm("Search", "Book", FormMethod.Get))
    {
        @Html.TextBox("query")
        <input type="submit" value="Search" />
    }
}
weston
  • 54,145
  • 21
  • 145
  • 203