--view of MVC Elementary Knowledge and Controller's explanation

Source: Internet
Author: User

Controller is a more important part of MVC. Almost all of the business logic is handled here, and the data is taken out of the model. In ASP. PREVIEW5, the original controller class is divided into the controller class and the Controllerbase class. The controller class inherits from the Controllerbase class, and the Controllerbase implementation is the IController interface.

Controllerbase implements the Execute method of the IController interface, which, after the route is matched to the controller, invokes the Execute method to enter the controller's processing. It also defines an abstract method Executecore method, which is called at the end of the Execute method. The controller class also defines a number of useful methods, and our new controller must inherit from this controller class. For example, we create a new admincontroller:

Let's talk about the more important action methods in the controller. In ASPNET MVC, the URL is mapped to an action in the controller, and then the matching action handles our business logic and returns to the view. The public method in the controller is treated as an action method. The action method typically returns a actionresult result. By default, the method name of the action method is the action name of the action (the action name refers to the part of the route that matches the URL of the action method.) For example, Url:home/index, where Index is the action name).

ASP. NET MVC provides us with several implementations of ActionResult

Most commonly used

The ActionResult action method returns the result of the ActionResult type.
Fileresult writes the contents of a file, in a derived class, in a specific way
Partialviewresult invokes the Executeresult method of the parent class viewresultbase.
Overrides the Findview method of the parent class.
Jsonresult. Returns the son result. Returned by the JSON method.

Not used

ViewResult. Renders the view page to the client. Returned by the view method.
Redirecttorouteresult. Redirect to another route. Returned by the Redirecttoaction and Redirecttoroute methods.
Redirectresult. Redirect to a different URL. Returned by the redirect method.
Contentresult. Return to normal content. For example, a string. Returned by the content method.
Emptyresult. If the action must return a null value, you can return the result. The controller does not implement the method, you can return to the new Emptyresult ();

Arguments to the action method

For example, we want to define an action method for editing the log in Admincontroller:

For URL:ADMIN/EDITPOST/2, the above parameters are automatically assigned a value of 2. When the route is matched, ASP. NET MVC automatically assigns a value to the action method's parameters according to the route's settings. Therefore, the previous ID parameter will be automatically assigned to 2, as long as the route configuration, the ID parameter must be specified, for example:

    • Viewstart.cshtml is a startup file that is executed before all view (. cshtml) is executed when the view file is rendered.
    • Note that the viewstart.cshtml is called when the view is rendered, and the code in _viewstart.cshtml is not triggered if a view is output in the same way that the Partialview (the distribution page does not call the master page).
    • If there is a viewstart.cshtm file with the same name in a directory of view, such as the home directory, then this _viewstart is also called, but the first call is the outermost _viewstart, And then the Viewstart in the home directory.

A complete view is a combination of layout and other local view, how to develop a layout?

@RenderBody0
When you create a view based on this layout page, the contents of the view are merged with the layout page, and the contents of the newly created view
is rendered between labels through the @renderbody () method of the layout page.

@Html. Partial ("_loginpartial")
You can call the local distribution page to render in the current region.

@RenderSection ("Footer", false)
The layout page also has the concept of section, which means that if a section is defined in a view template, it can be rendered separately.
Of course, you also define the section in the view, otherwise an exception will occur:

@section footer{
<div>hello word</div>
}

Example products based on the ASP. NET MVC 5.0 Framework, Source: Http://www.jinhusns.com/Products/Download

--view of MVC Elementary Knowledge and controller explanation

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.