New Xxcontroller inherited from: System.Web.Mvc.Controller
A controller can contain more than one action. Each action is a method that returns a ActionResult instance
A controller corresponding to a XxController.cs control file, corresponding to the view in a XX folder, for the Xxcontroller under the action corresponding view file. General case an action corresponds to a view page
The action method outputs the return value:
1.string: Return the response message string directly;
2.ActionResult: Return controller Result object
3.JsonResult: Return Json string
How the Controller "passes" the processed data to the view
Viewdata/viewbag/tempdata/model
Public ActionResult Index ()
{
Employee data = new Employee ();
Data. Name = "Zhang San";
Data. Age = 26;
Viewbag.username = "white child painting";
viewdata["UserName"] = "flower thousand Bones";
tempdata["UserName"] = "kill Criss";//Temporary data
return View (data); This line of code is actually equivalent to Viewdata.model=model
}
The output is:
Model is actually viewdata.model.
The first viewbag.username will be overwritten by the viewdata["UserName", and the final result will not output "white paint"
Because ViewData and viewbag are essentially "viewdatadictionary" types, and the data is shared between them, they only provide different ways to manipulate the syntax.
Controller passes data to view
Assigns a value to the viewdata/viewbag/tempdata/in the controller, or passes the object to the View method
There are viewdata/viewbag/tempdata objects in both the view and the controller, which are assigned to these objects in the controller after they are assigned values to the objects in the view.
MVC Basics-controller