ASP. net mvc learning 4. Add the SearchIndex page to the Controller to implement simple query Functions

Source: Internet
Author: User
Tags actionlink



1. Familiar with the MVC routing process. If the URL is directed to the corresponding Controller method

2. added the SearchIndex page for simple query.


Http: // localhost: 9898/Movies ,:

The HTML code corresponding to this path is: @ Html. ActionLink ("Edit", "Edit", new {id = item. ID })

Parameter description: The first parameter is linkText, the second parameter is the action method to be called, and the third parameter is the route data generated by an anonymous object:

Http: // localhost: 9898/movies/Edit/1 url path. will configure (App_Start \ RouteConfig. cs) Follow{controller}/{action}/{id}Format:{movies}/{Edit}/{1}    Default route Configuration:

{Controller =, action =, id =View Code

If we update URL: http: // localhost: 9898/Movies/Edit? ID = 1 This URL can also be directed to the Edit page just now according to the ActionResult Method

Then, view the Edit Method in the Controller:

ActionResult Edit (id = (movie = RedirectToAction (View Code

We noticed that the method has [] on it. This HttpPost indicates that only the Post request of the page will call this method. If [] is not added before the method, the default value [] indicates that the method is called during the Get request.

Here, make upGetAndPostMethod:



The. cshtml page corresponding to the ActionRestlt Method@model MvcMovie.Models.Movie Specifies that the Model in the view template is Movie. Let's take a look at the Helper methods in several views:

Html.LabelForHelper displays the field name ("Title", "ReleaseDate", "Genre", or "Price ").

Html.EditorForHelper loads HTML <input> Elements

Html.ValidationMessageForHelper displays the verification information for its corresponding properties



              = RedirectToAction(


ASP. net mvc model binder receives form data submitted on the page and creates a Movie object as the parameter of ActionResult Edit (Movie movie.


ModelState. IsValidMethod to check whether the submitted data can be edited or updated. If the data is valid, the movies data is saved to the instance of MovieDBContext (db ). MovieDBContextMediumSaveChangesMethod To save the data to the database. Save and redirect the page to the index page

If the stored data is invalid and does not comply with the rules,Html.ValidationMessageFor Error Message


<Script src => </script> <script> =. optional (element) |! </Script> <script> val = $ .. optional (element) |> = param [] & val <= param [</script>View Code


Add in webconfig:

/> <! -- Elements removed clarity --> </system. web>View Code

All HttpGet methods are similar. They Get a Movie object and pass it to the View. Get: The data on the page should not be changed.


   ActionResult SearchIndex( MovieGenre,  GenreLst =  List<>             GenreQry =  d =  SelectList(GenreLst);              movies =  m              (!= movies.Where(s => (                                 View(movies.Where(x => x.Genre ==


3. Add the following code to the SearchIndex. cshtml page:

  @Html.ActionLink(, , <p>, <input type= value= />        </p>

4. query page implementation:


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: 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.