ASP. NET mvc3 routing and multi-dataset return

Source: Internet
Author: User
1. routes in ASP. NET mvc3

This article does not introduce too much theoretical knowledge, as we can see in the global. asax. CS file:Code:

 
Routes. maproute ("default", // route name "{controller}/{action}/{ID}", // URL with parameters new {controller = "home ", action = "Index", id = urlparameter. optional} // parameter defaults );

Below we mimic the above to complete a route we will use, as shown below:

 
Routes. maproute (null, // you do not need to specify the route name "{controller}/{action}/{ID}", // URL with parameters new {controller = "ARCHIVE ", action = "Post", id = urlparameter. optional} // parameter defaults );

The URLs defined above are as follows:

    • /
    • /Archive/post/1

Here we will not detail the routing principle, routing sequence, and writing of various heavy loads. We will provide 2nd articles mainly for the following work.

2. Return of multiple datasets in ASP. NET mvc3

As mentioned above, in the previous article, we have implemented reading and displaying data on the home page. Next, When you click to read, you should jump to the details page to read the details. we add the Controller named archive in the previous method and create a view named "Post" for it.

Next, we need to read the data in the archive controller, because we areArticleThe user's comment must be displayed at the same time as the details. In this way, the article object and the articlecomment object are two entities. This article will re-create a class to return, as shown below:

 
Public class acmodel {// C #3 Automatic attribute public article {Get; set;} public ienumerable comment {Get; Set ;}}

Next, we obtain the required data and return the data as follows:

 
// Query by Article ID. The returned result is public actionresult post (int id) {// a single article entity var article = android. article. single (A =>. id = ID); // use the LINQ query. Comments is the ienumerable set var comments = from a in Android. articlecomment where. articleID = ID select a; acmodel model = new acmodel {Article = article, comment = comments}; return view (model );}

There may be a lot of direct learning ASP. net MVC is not very familiar with the LINQ syntax. If you are not familiar with it, you can look at the basic LINQ query operations (C #), because the pages we use later are often used.

It can be seen that this solves the problem of returning multiple datasets in a view in ASP. NET mvc3. We can display the data according to the attributes of the acmodel object. Of course, you can also solve this problem by using views and creating corresponding classes. Next, we will display it in the View:

 
// Model is a reference to an acmodel object. The front side has mentioned @ model Android. controllers. archivecontroller. acmodel @ {var article = model. Article; var comment = model. comment ;}

Show the title of the article: @ Article. Title. When the comment is displayed, because the comment is the ienumerable set, traversal is performed when the comment is displayed, as shown below:

 
@ Foreach (var c in comment) {<p> @ C. Comment </P>}

Because the data is displayed here, it is similar to the previous one, so here is a simple description. I hope that returning multiple datasets in ASP. NET will help you. In the next article, let's take a look at the page layout method in ASP. NET mvc3.

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.