I am trying to create a simple search box that results in something like http://www.example.com/Search?s=searchTerm I have the routing setup so that it accepts a url like this, and does the right thing. The problem I'm having is getting the form to create the querystring. I have tried many variations of:

<% using (Html.BeginForm("Search", "Home", FormMethod.Get, new { ???? })) {%>
<input id="submitSearch" class="searchBox" type="text" runat="server"/>
<input type="submit" value="Search" /> <%} %>

I'm not sure how to setup the Html.BeginForm so it grabs the submitSearch value and passes it to /Search?s=valueHere. This seems like I am missing something easy.

share|improve this question

3  
why does your HTML <input> tag use the runat attribute? – David Sep 13 '10 at 15:13
feedback

1 Answer

up vote 6 down vote accepted

You need to set name on the input box to s.

<% using (Html.BeginForm("Search", "Home", FormMethod.Get, new { })) { %>
    <input id="s" name="s" class="searchBox" type="text" />
    <input type="submit" value="Search" />
<% } %>

Also, note that I changed the id to s as well, since common practice is to have the same value for name and id. However, it is only the name attribute that affects the query string name in the request.
And as David noted in a comment, the runat="server" is not needed in ASP.NET MVC.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.