ASP. net mvc 5 getting started tutorial (4) View and ViewBag, mvcviewbag

Source: Internet
Author: User

ASP. net mvc 5 getting started tutorial (4) View and ViewBag, mvcviewbag

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. net mvc 5 + EF 6 getting started tutorial (5) Model and Entity Framework

Download source code: Click here to download

View is used to display the data processed by the controller. In this section, we will look at how to access a View page through the controller and how to pass values from the controller to the View.

Open FirstController. cs in the Controllers folder, and change the code from the returned string to a View. The Code is as follows:

using System.Web.Mvc;namespace SlarkInc.Controllers{    public class FirstController : Controller    {        public ActionResult Index()        {            return View();        }    }}

Here, the Index Action of the Controller named First is executed and a View is returned.

The View corresponding to FirstController is in the First folder under the Views folder. The View corresponding to its Index Action should be the Index. cshtml file in the First folder. Therefore, we First delete the files in the First folder. Right-click the First folder and choose add> MVC 5 view with layout.

In the displayed project name, enter Index and click OK.

On the select layout page, retain the default value and click OK.

In this way, a View is created, and the file name is Index. cshtml.

The file content is as follows:

@{    Layout = "~/Views/Shared/_Layout.cshtml";}

This Code indicates that the template under this path is applied.

Right-click the Index. cshtml file and choose View in the browser.

Shows the running result. Note that the URL consists of the first two controllers, routes, and the knowledge in this section. We can summarize the call process of this page:

The browser sends a request through url: "http: // localhost: 57231/First/Index. The RouteConfig. cs parses the route and matches the rule url: "{controller}/{action}/{id}" to obtain the Index Action under the First Controller. Then, Index Action returns a View, whose corresponding View is the Index. cshtml file in the First folder under the Views folder. This file is loaded again "~ /Views/Shared/_ Layout. cshtml "template. Therefore, the following page is returned, and the content on the page is the template content.

If View does not accept any parameters, this page can only be a static page. ViewBag is used to transmit parameters to a Controller when calling a View.

ViewBag is a magical thing. It is a dynamic object. Simply put, you can add any attribute to it, which can be of any type.

The main purpose of ViewBag is to add attribute values to ViewBag in the Controller and read and display these values in the View.

Step 1: add some data to ViewBag in FirstController. cs. These data types are different.

using System.Collections.Generic;using System.Web.Mvc;namespace SlarkInc.Controllers{    public class FirstController : Controller    {        public ActionResult Index()        {            ViewBag.Number = 8;            ViewBag.Message = "This is index Page";            ViewBag.Slarks = new List<string> { "Slark1", "Slark2", "Slark3" };            return View();        }    }}

Then read the data in Index. cshtml:

@{    Layout = "~/Views/Shared/_Layout.cshtml";}<p>Number: @ViewBag.Number</p><p>Message: @ViewBag.Message</p>@{    foreach (string slark in ViewBag.Slarks){        <p>@slark</p>    }}

The above code shows two different types of code: Razor code with @ {} and HTML code. In a cshtml, the C # code of the Razor package is executed on the server side. After the execution is complete, the output HTML page is returned to the client. The running result is as follows.

We can see that the value from ViewBag is displayed in View.

Your suggestion and message are my motivation for writing. Thank you.

Next section: ASP. net mvc 5 + EF 6 getting started tutorial (5) Model and Entity Framework

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.