如何将文本框值传递到asp.net mvc中的查询字符串中

时间:2022-12-02 09:59:45

Yet another newbie with ASP.NET MVC! All I intend to do is for a search textbox on my page, if I type something and click search, the url to be redirected to should have the following at the end, like in *,

ASP.NET MVC的另一个新手!我打算做的只是在我的页面上搜索文本框,如果我输入内容并单击搜索,要重定向到的url应该在结尾处具有以下内容,例如*,

/search?q=searchedtext

So here is what I have now,

所以这就是我现在所拥有的,

<input id="searchText" maxlength="100" type="text" name="query" />
    <a href="???"  class="searchButton">        
        Search        
    </a>

I have a function in my controller like this,

我在我的控制器中有这样的功能,

public ActionResult Search(string query)
 {

 }

Here is the route,

这是路线,

routes.MapRoute(
            "Search",                                            
            "Search",                          
            new { controller = "Posts", action = "Search"} 
        );

Can anyone fill in the gaps here :). Any comments appreciated.

谁能填补这里的空白:)。任何评论赞赏。

3 个解决方案

#1


Wrap it with form tag, set it's method to "GET", use input type='submit' for submitting form (instead of 'a' tag), name text input as query (already done), accept query as string in parameters (already done), call model from controller to process request, update ViewData.Model, return appropriate view result (partial, if AJAX is used).

用form标签包装它,将它的方法设置为“GET”,使用input type ='submit'提交表单(而不是'a'标签),将文本输入命名为query(已经完成),在参数中接受查询作为字符串(已完成),从控制器调用模型到处理请求,更新ViewData.Model,返回适当的视图结果(部分,如果使用AJAX)。

If you want to pass query through URL not through query string key/values,
you must specify correct route for that.

如果要通过URL而不是通过查询字符串键/值传递查询,则必须为此指定正确的路由。

I guess that would be something like:

我想这会是这样的:

routes.MapRoute(  
            "Search", // Route name  
            "search/{query}", // URL with parameters  
            new 
              { controller = "search", action = "search"}  // Parameter defaults
        );

#2


You need to wrap that client side code in a form:

您需要将该客户端代码包装在一个表单中:

<form action="/Search" method="get">
   <input id="q" name="q" maxlength="100" type="text" />
   <input type="submit" id="submit" value="Search" />
</form>

#3


here is what i did:

这是我做的:

<% Html.BeginForm("Search", "controller name", FormMethod.Get); %>
        <input id="criteria" name="criteria" maxlength="120" type="text" style="width:120px; " />
        <input type="image" alt="search" />
    <% Html.EndForm(); %>

public ActionResult Search(string criteria)
    {
       //search code goes here
    }

#1


Wrap it with form tag, set it's method to "GET", use input type='submit' for submitting form (instead of 'a' tag), name text input as query (already done), accept query as string in parameters (already done), call model from controller to process request, update ViewData.Model, return appropriate view result (partial, if AJAX is used).

用form标签包装它,将它的方法设置为“GET”,使用input type ='submit'提交表单(而不是'a'标签),将文本输入命名为query(已经完成),在参数中接受查询作为字符串(已完成),从控制器调用模型到处理请求,更新ViewData.Model,返回适当的视图结果(部分,如果使用AJAX)。

If you want to pass query through URL not through query string key/values,
you must specify correct route for that.

如果要通过URL而不是通过查询字符串键/值传递查询,则必须为此指定正确的路由。

I guess that would be something like:

我想这会是这样的:

routes.MapRoute(  
            "Search", // Route name  
            "search/{query}", // URL with parameters  
            new 
              { controller = "search", action = "search"}  // Parameter defaults
        );

#2


You need to wrap that client side code in a form:

您需要将该客户端代码包装在一个表单中:

<form action="/Search" method="get">
   <input id="q" name="q" maxlength="100" type="text" />
   <input type="submit" id="submit" value="Search" />
</form>

#3


here is what i did:

这是我做的:

<% Html.BeginForm("Search", "controller name", FormMethod.Get); %>
        <input id="criteria" name="criteria" maxlength="120" type="text" style="width:120px; " />
        <input type="image" alt="search" />
    <% Html.EndForm(); %>

public ActionResult Search(string criteria)
    {
       //search code goes here
    }