tags:

views:

14

answers:

1

I have an input box where in the value entered will be POSTed to the Action parameter. But I also want the value to reflect on the url like this:

www.my-site.com/search/myquery

Any idea on how to achive this?

Here are the codes..

Search Form



        <% using(Html.BeginForm("Index", "Search")) { %>

            <%: Html.TextBox("query", "Enter Keyword") %>

        <% } %>

Global Asax


routes.Add("Search", new Route(
                "Search/{query}",
                new { controller = "Search", action ="Index", query="" }
            ));


Controller



 public ActionResult Index(string query)
        {
return new EmptyResult()
    }


+1  A: 

If you want to pass the value entered by the user into the query string you need to use javascript. Register for the submit event of the form and append it to the url. Another possibility is to use the GET verb instead of POST which will automatically append it to the query string (but in this case you should use a different action name because you cannot have two actions with the same name and same verb).

<% using (Html.BeginForm("Index", "Search", FormMethod.Get)) { %>
    <%: Html.TextBox("query", "Enter Keyword") %>
    <input type="submit" value="OK" />
<% } %>
Darin Dimitrov