Three ways that MVC controllers pass values to view views

Source: Internet
Author: User

First create an MVC project, which requires a controller (TestController), three Views (Index,edit,detail)

1, the project structure is as follows:

2, controller to view the value of three ways

1) Provide view model object (directly put object in view)

2) passing data through Vewbag

3) passing data through ViewData

3, the controller source code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;

Namespace Mvctest.controllers
{
public class Testcontroller:controller
{
<summary>
1. Providing view Model objects
</summary>
<returns></returns>
Public ActionResult Index ()
{
DateTime date = DateTime.Now;
An object is passed to the view as a parameter of the view method
return View (date);
}
<summary>
2. Using ViewBag (view package) to pass data
</summary>
<returns></returns>
Public ActionResult Edit () {
ViewBag allows you to define any property on a dynamic object and access it in a view. This dynamic object can be accessed through the Controller.viewbag property.
Viewbag.name = "Timely rain";
Viewbag.age = "21";
Viewbag.hobby = "Playing basketball, reading books, etc.";
Viewbag.strdate = DateTime.Now;
return View ();
}
<summary>
3. Using view data to pass data
</summary>
<returns></returns>
Public ActionResult Detail () {
Before MVC3.0, the main way to pass data is by using the Viewdatadictionary class instead of a dynamic object.
The Viewdatadictionary class is similar to the standard "key/value" collection and is accessed through the ViewData property of the Controller class. This method requires that the object be transformed in the view.
viewdata["Message"] = "hello,everyone!";
viewdata["Date"] = DateTime.Now;
return View ();
}
}
}


4. Calls to Views

1) Index View

2) Edit View

3) Detail View

5, the display effect of the page

1) index page

2) Edit Page

3) Detail Page

Ps: The view to the controller value, generally through the AJAX Request controller method, the request is to take the parameters into the controller method parameters

Three ways that MVC controllers pass values to view views

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.