MVC Basics-controller

Source: Internet
Author: User

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

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.