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