一个搜索框添加到母版页 [英] Add a search box to a master page

查看:165
本文介绍了一个搜索框添加到母版页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个搜索框在一个ASP.Net MVC的Web应用程序添加到母版页。什么是困惑我是如何正确地执行它的母版页。因此,用户在数据到这个搜索框,应该如何将数据从一个MVC的角度来处理?我知道我可以用他的母版页的code落后,但我不应该。我目前正在尝试使用该用户的控制,但我不知道如何正确地执行它和在线资源似乎被限制。将创建一个HTML帮助是最好??

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??

要总结:在引导到不同的网站,包括他们在搜索框中键入用户的查询的MVC母版页实现一个搜索框

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.

是更好的使用方法:


  • 母版页的codebehind

  • 用户控件

  • 或者创建一个单独的HTML辅助。

更新:

好吧,每个queen3的建议下,我实现了一个SearchController和使用的HTML帮助BeginForm生成一个搜索框。

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

控制器动作:

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

和在母版页:

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

但是,当我尝试调试时,SearchWiki的功能不会被调用,作为一个结果,没事的时候我在搜索框中键入和命中的搜索情况。

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.

推荐答案

忘记codebehind和用户控件,如果你要使用ASP.NET MVC。你需要HTML,CSS和JavaScript。

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

我想你想是这样

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

通过助手它会像

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

只是把这个变成母版页在适当情况下,你的网站设计。然后创建SearchController处理的要求,在搜索结果中返回查看()。你可以让你接受类似谷歌的搜索请求/搜索表单使用GET而不是POST?Q =文本。

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.

控制器是非常简单的:

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
  }
}

这篇关于一个搜索框添加到母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆