Step 7 of ASP. NET mvc4.0 Getting Started Guide-add search methods and views

Source: Internet
Author: User
Tags actionlink

Note: The content in this section is actually the second half of the sixth part of the original English tutorial. Because the content in section 6 is long, the content in this section is related to the title of the previous section (add the Edit Method and view) irrelevant. The search function is both common and commonly used in project development. It is used as a separate chapter here.

In this course, you will add a searchindex method so that you can search for movies by name. The/movies/searchindex address will be available. This request will display a form containing the input elements that users can input to find the video. After a user submits a form, this method obtains the search value submitted by the user and is used to search the database.

First, add a searchindex Method to the existing moviescontroller class. This method returns a view containing an HTML form. The following code is used:

Public actionresult searchindex (string searchvalue) {// query all movies in the LINQ mode. It is defined only and VAR movies = from m in dB is not executed. movies select m; If (string. isnullorempty (searchvalue) = false) {// a Lambda expression based on the query results of LINQ. Call the where method and execute LINQ movies = movies. where (M => M. name. contains (searchvalue);} return view (movies );}

The query operation is delayed, meaning that the value operation of the expression will not be executed until the value is traversed or the tolist method is called. In this example, the query is executed in the searchindex view. For more information, see query execution.

Now you can implement the searchindex view to present user forms. Right-click the searchindex method and choose add view from the shortcut menu. In the dialog box, select "Create strong type view" and specify that you will pass the movie object to the view template as its model class. In the stent template list, select list and click Add, as shown in

After you click the Add button, the view template views \ movies \ searchindex. cshtml will be created. Because you have selected the list in the bracket template, Visual Studio automatically generates some default tags in the view. The support system views the movie class and creates <label> attribute elements for each of its attributes to form an HTML form. The following is a view file:

View code

@model IEnumerable<MvcMovie.Models.Movie>@{    ViewBag.Title = "SearchIndex";}

Run the program, navigate to/movies/searchindex., and append a URL similar to "? The query string of searchvalue = juvenile. The filtered results are displayed.

If you change the searchindex method signature so that the parameter identifier is ID, the parameter ID will be. the default route set in the asax file matches {controller}/{action}/{ID}, that is, the searchvalue parameter of searchindex is changed to ID. Now you can pass the search name as the route data to replace the query string method, that is, access http: // localhost: 5279/movies/searchindex/juvenile in the following way.

However, you cannot expect users to modify the URL each time they search for a video. Therefore, you can add an interactive interface (UI) to help them filter videos. If you have modified the searchindex method signature in the previous section to test the ID parameter passed through the route binding method, you can change it back to that the method has a string type parameter named searchvalue.

Open the views \ movies \ searchindex. cshtml file and add the following code after @ html. actionlink ("create new", "CREATE") code:

@ Using (HTML. beginform () {<p> name: @ HTML. textbox ("searchvalue") <br/> <input type = "Submit" value = "query"/> </P>}

The HTML. beginform method creates the <form> tag. Click "query" to submit the form. Run the program and try to query the video.

Actually, there is no searchindex method marked as the httppost attribute. Because this method does not modify the application status and only filters data, it does not need to overload the httppost attribute.

Note: I cannot figure out a problem here. As described above, the default property of the searchindex method is httpget, and HTML is used in the view. beginform (). After running the program, check the page source code and find that the default value is <form action = "/movies/searchindex" method = "Post">, that is, after you click the button, the form is passed through the POST method. Why does it call the searchindex method that accepts the httpget method above? Is it because the httppost attribute is first retrieved and submitted to httpget for processing if it cannot be found? Is this the result of HTTP or MVC?

You can add the searchindex method of the following httppost attribute. In this case, the method call matches the httppost method, and the following method will be executed.

[Httppost] Public String searchindex (formcollection frm, string searchvalue) {return "

However, even if you have added the httppost method of searchindex, there are still restrictions when using it. Imagine you want to get a specific search bookmarks or you want to send a link to a friend who can click to view the same filtered movie list as you. Note that the URL of the POST request is exactly the same as the URL of the GET request, and no information is searched in the address bar. The text information searched is sent to the server as the form field value. Therefore, you cannot obtain bookmarks containing search information or send a link to a friend.

The solution is to use the inform overload method to specify the POST request to add the search information to the URL address and route it to the searchindex method of httpget. Use the following code to replace the existing ininform method:
@ Using (html. beginform ("searchindex", "Movies", formmethod. Get ))

Now, when you submit a search request, the URL will contain the query string. Even if the searchindex method of the httppost attribute exists, the search will be processed by the searchindex method of the httpget attribute.

Note: The above is the original translation result. The translator feels that there is a logical conflict. Use HTML. specifies the formmethod. the get parameter, that is, changing the form submission method from the default POST method to get, will naturally call the searchindex method of the httpget attribute.

Note: the original article further provides an example of using the drop-down list of the video style as the query condition. It is similar to the most, but the details are slightly different. You can delete the sections, for more information, see the original document.

In this section, you have created a search method and view, allowing you to search for a database using a video name. In the next section, you will learn how to add an attribute to the movie model and how to add an initiator to automatically create a test database.

 


Navigation of all articles in this tutorial

This series contains 10 articles translated from ASP. net mvc4 official tutorial, due to the concise description of this series of articles, the length is moderate, from an example to explain, the full text finally completed a small system for managing movies, very suitable for beginners ASP.. Net mvc4.

The original article is for 9 articles, and the translator splits 6th of them into 2

1. Introduction to ASP. NET mvc4

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

· Address: http://www.cnblogs.com/seawaving/archive/2012/12/03/2800210.html

2. Add a controller

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-controller

· Address: http://www.cnblogs.com/seawaving/archive/2012/12/04/2801949.html

3. Add a view

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view

· Address: http://www.cnblogs.com/seawaving/archive/2012/12/04/2801988.html

4. Add a model

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model

· Address: http://www.cnblogs.com/seawaving/archive/2012/12/05/2803012.html

5. Access the data model from the Controller

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller

· Address: http://www.cnblogs.com/seawaving/archive/2012/12/05/2803429.html

6. view the Edit Method and edit View

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view

· Address: http://www.cnblogs.com/seawaving/archive/2012/12/05/2804100.html

Http://www.cnblogs.com/seawaving/archive/2012/12/06/2804590.html

7. Add fields for the movie model and database table

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table

· Address: http://www.cnblogs.com/seawaving/archive/2012/12/06/2805401.html

8. Add verification for the model

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model

· Address: http://www.cnblogs.com/seawaving/archive/2012/12/06/2806322.html

9. view the detail and delete Methods

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and-delete-methods

· Address: http://www.cnblogs.com/seawaving/archive/2012/12/10/2811064.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.