ASP. NET MVC Light Tutorial Step by Step 3--using ViewBag

Source: Internet
Author: User

In the previous section, we created the index view corresponding to the index action method, so how can the index action method transfer the data to the index view? One method is to use the ViewBag (view package). Let's try it.

Add a line of code to the Index action method.

        Public ActionResult Index ()        {            = DateTime.Now.ToLongDateString ();             return View ();        }

Today is our own name and looks like a property of ViewBag, but ViewBag is a dynamic object and we don't have to declare today.

In the index view, to display the viewbag today, we need to add the following code.

<body>    <H1>MVC Message Board     @ViewBag. Today</body>

After the ASP. NET MVC3 version, view uses the Razor view engine, characterized by the @ symbol as the token-code conversion character. Simply put, in the HTML markup to write C # code, in the C # code before adding the @ symbol, more complex situation we encountered a further explanation.

ViewBag is a dynamic object, so there is no code hint, you must manually call "Today" out.

Once run, we can see the current system date passed to the view by the index method.

We can even transfer the content of the message to the index view via ViewBag and display it. In the index action method we add a viewbag.

        Public ActionResult Index ()        {            viewbag.today = DateTime.Now.ToLongDateString ();             = new string[] {"Test data 1", "Test Data 2", "Test Data 3", "Test Data 4", "Test Data 5", "Test Data 6", "Test Data 7", "Test Data 8"};             return View ();        }

In the index view, to display the contents of an array of viewbag.message strings, we need to write more code.

<body>    <H1>MVC Message Board     @foreach (var s in viewbag.message)    {        <p>@s</p>    }</body>

With a foreach loop, you get all the strings in viewbag.message, and note the mix of code and tags here.

See the results after running.

One of the drawbacks of using ViewBag to pass data is that there is no code hint for dynamic data, and handwriting is guaranteed to be correct and not suitable for the delivery of complex objects. If you just send a simple string, the viewbag is still very useful.

ASP. NET MVC Light Tutorial Step by Step 3--using ViewBag

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.