MVC pass Value--passing the list collection from controller to view

Source: Internet
Author: User

in the in MVC, passing values is one of the most important things. In the previous blog, we learned how to pass a single variable value from a controller to a view and a reverse pass from view to the controller.

This time we'll see how to pass a list collection.

First, let's start by creating a list in the controller:

        Public ActionResult Index ()        {            viewdata["QuestionID"] = request.querystring["QuestionID"];// Get QuestionID from the address bar of the page            //Query the test bank information according to the question ID            list<examquestionbankentity> questionbanklist = new list< Examquestionbankentity> ();            Examquestionbankentity Enquestionbank = new examquestionbankentity ();//definition of an entity            Examquestionbankservice = Examservicefactory.getquestionbankservice ();//Get service            list<string> QuestionID = new list<string> ();            Questionid.add (viewdata["QuestionID"). ToString ());//Get question id            questionbanklist = examquestionbankservice.queryquestioninfobyquestionguidlist ( QuestionID)///Execute Query method            viewdata["questionbanklist"] = questionbanklist;//to interface with, display specific questions of the test information            return View ();        

To display the information in the collection in view, first refer to "entities" in the entity collection, and here I am viewmodel. This is followed by a loop for parameter passing.

In view:

@using Itoo. Exam.viewmodel; <! DOCTYPE html> 

Here I show the various values in the specific list in a circular way. Strictly speaking, this is not a very good way, because my list is guaranteed to have only one piece of data, so the list is not really suitable.

This method is more suitable for the application in the table, through the loop to display the data, here does not show the code.

There are other ways to pass a list through the controller to the view. The other method I know and used is more complicated, the operation of ViewModel is rather complicated, and no loop is used, but it is mainly used to display tabular data. Specific is not here to write, we are interested in the Internet to check.

There will be a summary of other things about the MVC value, so please look forward to

MVC pass Value--passing the list collection from controller to view

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.