views:

65

answers:

2

I'm trying to add a search box to a master page in an ASP.Net MVC web app. What's confusing me is how to properly implement it in the master page. So the user types in data into this search box, how should the data be handled from an MVC perspective?? I know I could use he master page's code behind, but I shouldn't. I'm currently trying to use a user control for this, but I'm not sure how to properly implement it and online resources seem to be limited. Would creating an HTML helper be best??

To summarize: Implement a search box in the MVC master page that directs to a different website and includes the user's query that they typed in the search box.

Is it better to use:

  • Master Page's codebehind
  • A user control
  • Or create a separate HTML Helper.

UPDATE:

Ok, per queen3's advice, I implemented a SearchController and used the HTML Helper BeginForm to generate a search box.

Controller action:

        Function SearchWiki(ByVal q As String) As ActionResult
            Return Redirect("http://home/search/Results.aspx?k=" & q & "&s=IT%20FAQ")
        End Function

And in the Master Page:

<% Using Html.BeginForm("SearchWiki", "Search", FormMethod.Post)%>
                                <input type="text" name="q" />
                                <input type="submit" value="Search" />
                            <% End Using%>

But when I try to debug, the SearchWiki function never gets called and, as a result, nothing happens when I type in the search box and hit Search.

+3  A: 

Forget about codebehind and user controls if you're going to use ASP.NET MVC. You need HTML, CSS, and JavaScript.

I suppose you want something like

<form action="<%= Url.Action("Index", "Search") %>" method="post">
   <input type="text" name="q" />
</form>

With helpers it will be something like

<% Html.BeginForm("Index", "Search") %>
   <input type="text" name="q" />
<% Html.EndForm() %>

Just put this into master page where appropriate in you site design. Then create SearchController to handle request, and return View() with search results. You may make form use GET instead of POST if you accept google-like search requests /Search?q=text.

The controller is very simple:

public class SearchController: Controller
{
  public ActionResult Index(string q)
  {
    return View(SearchHelper.DoSearch(q));
    // or return Redirect("http://site?q=" + q) if you want redirect
  }
}
queen3
+2  A: 

To summarize: Implement a search box in the MVC master page that directs to a different website and includes the user's query that they typed in the search box.

Seems like you want to use a different search provider. In this case you don't need any server-sided code at all... only pure html. I'll give you an example with Google:

<form id="search" action="http://www.google.com.br/search" method="GET">
    <input type="text" name="q" />
    <input type="submit" value="Submit" />
</form>

Just add this code on your MasterPage and we're done.

You can also add some JQuery to append the string "site:www.yoursite.com" to the search query. Doing so you can ask google to search the keywords on your site. The javascript code should be:

$("#search").submit(function(){
    var input = $(this).find('input[name=q]');
    var query = input.val() + ' site:www.yoursite.com';
    input.val(query);
});
Rodrigo Waltenberg