views:

211

answers:

1

I have a simple form for searching for a user

<p>Enter a user's id number to search:</p>

<%  using (Html.BeginForm("Search", "UserAdmin", FormMethod.Get)) { %>

        <%= Html.TextBox("id") %>

        <input type="submit" value="Search" />
<% } %>

I want this link to go to useradmin/search/{id} but the link is rendered as useradmin/search?id={id}

Both urls are valid and map to my action as I would expect, I just think the former is neater and want that style to be used.

Update:

Based on Michael Gattuso's answer I have this as a working solution. Not elegant, but it works.

<p>Enter a user's id number to search:</p>

<%  using (Html.BeginForm("SearchPost", "UserAdmin")) { %>

        <%= Html.TextBox("id") %>

        <input type="submit" value="Search" />
<% } %>


    public ActionResult Search(string id)
    {
        var result = _Service.SearchForUsers(id);
        return View(result);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult SearchPost(string id)
    {
        return RedirectToAction("Search", new { id = id });
    }
A: 

This isn't an MVC issue. A form request using GET will append the inputs to the query string as per the rendering you get. There is nothing in MVC that would fix this as the request does not go through the routing engine. Your option is either POST the form and route it or use javascript.

UPDATE

To do this in MVC use a form POST

<p>Enter a user's id number to search:</p>

<%  using (Html.BeginForm("Search", "UserAdmin", FormMethod.Post)) { %>

        <%= Html.TextBox("id") %>

        <input type="submit" value="Search" />
<% } %>

With a corresponding Action:

[AcceptVerb("Post")]
[ActionName("Search")] //I assume your current search action has this same signature so use alias
public ActionResult SearchPost(int id){
  return new ActionResult("Search", new { Id = id });
}
Michael Gattuso
I've definitely seen the behavior I want done somewhere using MVC, I just can't remember exactly where :\
Kirschstein
When you use a form GET request the browser itself comes up with the new url - http://example.com/useradmin/search?id={id} before it even touches the server. To get the routing you want you have to POST the form and process it through the routing engine
Michael Gattuso
Interesting - Attempting this but can't seem to find alias. is that a beta2 attribute?
Kirschstein
Sorry - I meant ActionName not Alias (works in MVC 1) I was doing it from memory. I have updated. I would recommend, however, that do what you want in javascript and let it naturally degrade to the way it's working right now. That way you don't even need the extra Action in your controller.
Michael Gattuso