This section focuses on how to pass data in the Controller to the view. If you are not clear about the previous content, refer to Asp.net MVC-view and Page Template for networking
Before using the database and introducing the model, we will first introduce how to pass the information in the Controller to the view. After receiving a URL request, the browser calls the Controller class to respond. You can process the received page parameters in the Controller class.CodeYou can write the code for getting data from the database in the Controller class, or you can write the code in the Controller class to decide the format of the response file returned to the client. The controller can use the view template file to generate HTML-format response files and display them in the browser.
The Controller class is responsible for providing any data or objects required by view template files when generating HTML response files. A view template file should not execute any business logic or directly interact with the database. It can only interact with the Controller class and obtain the data provided by the Controller class. This makes your code clearer and easier to maintain.
Now in our applicationProgramThe welcome method in the helloworldcontroller class has two parameters-Name and numtimes. The welcome method directly outputs the parameter values of these two parameters to the browser. Here, we modify this method so that it does not directly output data, but uses a view template. This view template will generate a dynamic response stream, which means we need to pass the data from the Controller class to the view so that the data can be used to generate the response stream. In this controller class, we send the data required by the view template to a viewbag object, which can be directly received by the view template.
Open helloworldcontroller. CS file, modify the welcome method, add a message attribute and numtimes attribute to the viewbag object in this method, and set the attribute value to the processed name parameter value and numtimes parameter value respectively. A viewbag object is a dynamic object. You can add any attribute and assign the attribute value to it. This attribute does not take effect until you assign a value. The code in the modified helloworldcontroller. CS file is shown in code list 3-4.
Code List 3-4 The modified helloworldcontroller. CS File
Now the viewbag object contains data, which is automatically passed to the view.
Next, we need to create a welcome view template. In the Debug menu, click Generate mvcbooks to compile the application.
Next, right-click the welcome method and click "add view". In this dialog box, do not modify the view. Click the Add button directly, the helloworld file in the view folder is automatically created with a welcome. open the cshtml file and add code under the <H2> element to display the text set in the name parameter passed in the URL in the browser, the number of times displayed is equal to the number of times specified in the numtimes parameter passed in the URL. The code in the modified welcome. cshtml file is shown in the code list 3-5.
Code List 3-5 the modified welcome. cshtml File
Run the application and enter "http: // localhost: XX/helloworld/welcome?" in the address bar? Name = foxitjob.com & numtimes = 4 ", the page parameters in the address bar will be automatically passed to the Controller. The Controller will put these parameter values in the viewbag object and pass them to the view. The view then displays the data in the browser.
Here, we use the "M" model, but not the database method. In the next section, we will create a database and describe how to process the data in the database.