Learn about ASP. NET MVC5 Official Tutorials Summary (eight) search query

Source: Internet
Author: User
Tags actionlink

Learn about ASP. NET MVC5 Official Tutorials Summary (eight) search query

In this section, we add query functionality to the Index method so that we can find it based on the subject matter or name of the movie.

First, we need to update the Index method of Moviescontroller, the code is as follows:

Public ActionResult Index (string searchstring)        {            var movies = from M in db. Movies select M;            if (! String.IsNullOrEmpty (searchstring))            {                movies = movies. Where (s = = S.title.contains (searchstring));            }            Return View (Movies);        }

The first line of code for the Index method creates a LINQ query to select a movie that matches the criteria:

var movies = from M in db. Movies Select M;

Although this query is defined here, it is not executed in the database.

If the searchstring parameter contains a string (not an empty string),themovies Query adds a filter for the query string, with the following code:

if (! String.IsNullOrEmpty (searchstring))            {                movies = movies. Where (s = = S.title.contains (searchstring));            }

The s = = S.title in the code is a lambda expression that is used in a method-based LINQ query (in the code above where method), as a parameter to use. LINQ statements are not executed when they are defined or modified, instead, the query is deferred, which means that an assignment statement is made until the iteration is complete or called The ToList method has a true value. In the above example, the query statement is executed in the index.cshtml view.

Now, you can modify the Index view to show him a form for user input.

If you change the parameter name of the Index method to an ID, then the ID parameter will match app_start\ {ID} in the default route in the RouteConfig.cs file

{Controller}/{action}/{id}

The revised Index method is as follows:

Public ActionResult Index (string id)        {            string searchstring = ID;            var movies = from M in db. Movies select M;            if (! String.IsNullOrEmpty (searchstring))            {                movies = movies. Where (s = = S.title.contains (searchstring));            }            Return View (Movies);        }

Once modified, we can pass the query string by routing the data.

However, you can't expect users to find movies every time they modify URLs , so you need to help them filter the data on the interface.

Open the Views\movies\index.cshtml file and add the following code after @Html. ActionLink ("Create New", "create") :

@Html. ActionLink ("Create New", "create")    @using (Html.BeginForm ())    {    <p>        Title: @ Html.textbox ("searchstring") <br/>        <input type= "Submit" value= "Filter"/>    </p>    }

Html.BeginForm Help method to create a <form> tag. submit the form to the current page by clicking the Filter button.

Then, we modify The code of the Index method so that it can query according to the subject matter. The modified code is as follows

public    ActionResult Index (String moviegenre, String searchstring) {var genrelst = new list<string> (); var genreqry = from D in Db.    Movies d.genre Select D.genre;    Genrelst.addrange (Genreqry.distinct ());    Viewbag.moviegenre = new SelectList (GENRELST); var movies = from M in db.    Movies Select M; if (! String.IsNullOrEmpty (searchstring)) {movies = movies.    Where (s = = S.title.contains (searchstring)); } if (!string. IsNullOrEmpty (moviegenre)) {movies = movies.    Where (x = x.genre = = moviegenre); } return View (Movies);} 

After modifying the controller, we also need to add a field in the Index view. Before we query the name, use html.dropdownlist to Add a drop-down box, the modified code is as follows:

@using (Html.BeginForm ("Index", "Movies", Formmethod.get)) {<p>    Genre: @Html. DropDownList ("Moviegenre", " All ")    Title: @Html. TextBox (" searchstring ") <br/>    <input type=" Submit "value=" Filter "/></p }

The code @Html. DropDownList ("Moviegenre", "all") generates a drop-down list, and the parameter moviegenre indicates the ViewBag to find The name of the data collection in the.

In this chapter, we create methods and views of the query so that users can query the title and subject matter of the movie. In the next chapter, we will explain how to add attributes to the Movie model and how to add an initial value that automatically creates a test database.
















Learn about ASP. NET MVC5 Official Tutorials Summary (eight) search query

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.