Original: ASP. NET MVC 5 Getting Started Tutorial (4) View and ViewBag
Article Source: slark.net-Blog Park http://www.cnblogs.com/slark/p/mvc-5-get-started-view.html
Previous section: ASP. NET MVC 5 Getting Started Tutorial (3) Routing route
Next section: ASP. MVC5 + EF6 Getting Started Tutorial (5) Model and entity Framework
SOURCE download: Click I download
View is used to display data that is handled by the controller. In this section, we'll look at how a controller accesses a view page, and how to pass a value from a controller to a view.
Opening the FirstController.cs in the Controllers folder, we changed the code from returning a string to returning a view. The code is as follows:
using SYSTEM.WEB.MVC; namespace slarkinc.controllers{ publicclass firstcontroller:controller { Public actionresult Index () { return View ();}} }
This indicates that the index action of the controller named first returns a view after execution.
Firstcontroller the corresponding view is within the first folder under the Views folder. The view for which the index action corresponds should be the index.cshtml file within the first folder. Therefore, we first delete the files in the primary folder. Then right-click on the first folder to select Add-on MVC 5 view page with layout.
Enter index in the name of the project that pops up and click OK.
In the Select Layout page, leave the default and click OK.
In this way, a view is created and the file name is index.cshtml.
The contents of the file are as follows:
@{ "~/views/shared/_layout.cshtml";}
This code indicates that the template under this path is applied.
Right-click on the index.cshtml file and select View in browser.
The result of the operation is as shown. Note The URL, which consists of the first two sections of the controller, routing, and knowledge of this section, we can summarize the invocation process for this page:
The browser makes the request via the URL: "Http://localhost:57231/First/Index". The route is parsed by RouteConfig.cs, which matches the rule url: "{controller}/{action}/{id}", which gives access to the index action under the first controller. The index action then returns a view whose corresponding view is the index.cshtml file in the first folder under the Views folder. This file also loads the "~/views/shared/_layout.cshtml" template, so it returns the following page, which is the content of the template.
If view does not accept any parameters, then this page can only be a static page. To pass arguments to a controller when it calls view, use ViewBag.
ViewBag is a very magical thing, it is a dynamic object. The simple point is that you can add any property to it, and this property can be any type.
The main purpose of ViewBag is to add attribute values to ViewBag in the controller and to read the values in the view and display them.
In the first step, we add some data into the viewbag in FirstController.cs. These data types are different.
usingSystem.Collections.Generic;usingSYSTEM.WEB.MVC;namespaceslarkinc.controllers{ Public classFirstcontroller:controller { PublicActionResult Index () {Viewbag.number=8; Viewbag.message="The IS index Page"; Viewbag.slarks=Newlist<string> {"Slark1","SLARK2","Slark3" }; returnView (); } }}
Then read the data in index.cshtml:
@{ "~/views/shared/_layout.cshtml";} <p>number: @ViewBag. Number</p><p>message: @ViewBag .message</p>@{ foreach (string in viewbag.slarks) { <p> @slark </p> }}
From the code above you can see two different kinds of code, one is the razor code with @{}, and the other is the HTML code. In a cshtml, the C # code of the razor package is executed on the server side, and the output HTML page is returned to the client after execution is complete. The results of the operation are as follows.
You can see that the values from ViewBag are displayed in the view.
Your recommendation and message is my motivation to write, thank you.
Next section: ASP. MVC5 + EF6 Getting Started Tutorial (5) Model and entity Framework
ASP. NET MVC 5 Getting Started Tutorial (4) View and ViewBag