How to pass a text field value to a query string in asp.net mvc

Another newbie with ASP.NET MVC! All I intend to do is search for a text field on my page, if I type and click on search, the URL to be redirected should have the following at the end, for example, in stackoverflow,

/search?q=searchedtext

So here is what I have,

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

I have a function in my controller, for example,

public ActionResult Search(string query)
 {

 }

Here is the route

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

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

+3
source share
3 answers

, "GET", input type='submit' ( "a" ), ( ), ( ), , ViewData.Model, (, AJAX).

URL- ,
.

, - :

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

:

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

:

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

Source: https://habr.com/ru/post/1711240/


All Articles