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