c# - How do I create a simple search box with a submit button to bring back a result set in MVC? -


I am very new to MVC and just learning the basics. I'm following the following in a stupid dinner and used the demo to make my own app. I have created a page which lists some foods with calories, fats, proteins etc. ... () This is one of my own personal sites which I have installed to test outside MVC. I'm getting a lot of work, but I'm stuck in a text box with a search button.

This code is for my watch page search:

  & lt; Form action = "/ calorie list / search" method = "post" id = "search form" & gt; & Lt; Input type = "text" name = "searchTerm" id = "searchTerm" value = "" size = "10" maximum length = "30" /> & Lt; Input type = "submit" value = "search" /> & Lt; / Form & gt;  

This code is for routing in my global .exad:

  route. The URL new {controller = "calorie list", action = "search", search = ""} // parameter with the map ("search", // root name "calorie list / search / {searchTerm}", // parameters Default);  

My Administrator has this code:

  Public Action Result Index (Anti-Page) {Contint Page Size = 10; // Load a list with the calorie list var calories list = calories LISTRepository.GetAllCalorieLists (); // var pagnetized calorie list = calorie list. Skip ((page ??) * page size). Move (Page Size) .Oolist (); Var paged calorie list = new paged list & lt; Calorie list & gt; (Calorie list, page ??, 0 page pages); See refund ("index", paged collar list); } Public Functional Search (String Search) {Contint PageSism = 100; Int? Page = 0; Var Calorie List = Calorie LISTRepository.GetCalorieListsBySearch (searchTerm); Var paged calorie list = new paged list & lt; Calorie list & gt; (Calorie list, page ??, 0 page pages); See refund ("index", paged collar list); } View return ("index", paged collarlist); }  

When I enter a value and click on the button, the indexant ignites the method instead of the method in the controller and I get the full list again. If I type url manually () I get the correct list.

Why is not my button using the correct writing and giving me search results?

Your form is actually indicating:

  Lt; Form id = "form1" action = "calorielist" method = "post" & gt;  

If you are using HTML Betform, you are passing in the wrong parameters.

  & lt;% use (Html.BeginForm ("Search", "Calorie List"))% & gt; & Lt;% {% & gt; // Your form field & lt;%}% & gt;  

One way to note, why is the viewstate on your page?


Comments