ASP MVC5 Introductory Learning series ⑥

Source: Internet
Author: User

Original: ASP. MVC5 Introductory Learning Series ⑥

Then the last time, we this article manual to write a query of the process code!

Implementation of the search/query process function

Now we're going to do the search (query) function. What should we do in the first step? The first time is whether we should go to the controller to get a search (query) method (action), then I say do the search function! Search may be based on the name of the search, if based on a certain criteria to search, then our method needs a parameter, if it is more than one parameter needs to pass multiple parameters to the method, so that the parameters of your method is not depending on your search criteria. Let's start with a simple single-condition query.

We do this search function in the Moivecontroller Index method (action), as follows:

The query syntax here is written in LINQ, which also contains a simple lambda expression, which, of course, is very common in the MVC Controller specific action method, for LINQ is not familiar with can go to the garden to find some information to see.

After writing this index method, this time the index method already has the search function, we run up to look:

Because we know its default routing mechanism is {Controller}/{action}/{id} ..., then we are now going to simulate HTTP to spell, here is mainly to verify the search method we write to see if it is valid, So now we're going to simulate HTTP spelling a request to access our search method, as follows:

Simulation request finished or not move, here is not my careless but intentionally write wrong, if you are careful, you will find the action in the back of the time is a problem, this is generally the most likely to make mistakes, In the first method, we all check the parameters according to the parameter, so it should be the result of our query instead of EF's strongly typed tolist (), so be careful here. Then we modify the method as follows:

After the change, we are repeating the operation of the above simulated HTTP request, looking at the result:

This is not OK, now our search method is written well, but we can not be the search is to go to the address bar to spell parameters to complete this operation, it is not a bit too frightened, So we're going to add a search box on our index.cshtml page for the user to input so it's a lot better, down here we'll do one thing, modify the index.cshtml code as follows:

Html.beginfrom is also a common attribute in MVC applications, which can submit the current from data in a specific way to the corresponding controller (the action) by committing a similar action from the from expression. Like ActionLink, it also has three parameters, but the Beginfrom parameter is more clear, the first argument is that you want to submit to the action/method, so here you give a handle or accept the method name; The second argument is where you're going to commit, You're going to have to hand over that controller to be responsible, so here you have to give the controller a name, and the third one is to choose the way you want to submit it.

Anyway, after index.cshtml add this thing, we run up and look at our homepage:

This kind of interface is not much more friendly.

Because the index method in our Moviecontroller you did not specify to accept the POST request or the GET request, so now manually write a method to accept the post request, and see what the POST request is going to do. Add one of the following methods in Moviecontroller, as follows:

Then we run up and look at the results of the execution, such as:

Of course the logic of the actual application may be more complex than this, so this depends on the situation, this time if you want your request results on the Index list page, it is actually very simple, Just change the index.cshtml we just wrote BeginForm FormMethod.Post to Formmethod.get, as shown below:

And then run the program, the effect into the following:

Now my search condition is slightly more complicated, for example I want to add according to genre (music type) and title (music name) to search together, and the music type is a selectable rather than I manually input, this time compared to the single condition query will be slightly more complicated, First you must dynamically read all the music types in the database and then the form of the following drop-down list for the user to choose, as long as we do this, then our goal is to complete 90%, so that the need to go to engage in such a thing, in order to pull the effect is obvious, I add some test data in the database, the following data:

After adding the data, we are now looking for ways to read the data and then dynamically bind to the page, then how do we do it! We only need to use the type of music to save the query in what way the results are more appropriate, it should be an array bar! Then we use the array to save the music type data read out, the specific operation is as follows:

Add a parameter, and then put the query all the music types into the collection through the dynamic dictionary accepted in the page binding, query/search when passing the music type come in not empty words to the database match, find data to return, said may not be very rigorous, but actually do the thing is so much. Let's see how the page is bound to this dynamic dictionary and become our expected dropdown box! The page is modified as follows:

Here DropDownList the first parameter is to accept the name of the return form, when we give the dynamic dictionary to him he will parse, the second parameter is displayed by default value This value we can give or can be null, edit the page, we run up to see the effect:

This is not to achieve the desired effect, casually query a result to see if you can query the results:

OK, the effect is realized if more parameters are the same, our search is done here, the subsequent words we add properties to the model, because our database is EF based on the model mapping out, so in some cases in the actual application of the model will have some small changes, Well, if it's a database that's mapped through EF code frist, we're going to throw in the usual problems with adding properties! After we continue to study, about the search we will first learn here.

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.