MVC basic knowledge-View and Controller, mvcviewcontroller

Source: Internet
Author: User

MVC basic knowledge-View and Controller, mvcviewcontroller

Controller is an important part of MVC. Almost all business logic is processed here and data is retrieved from the Model. In ASP. net mvc Preview5, the original Controller class is divided into the Controller class and the ControllerBase class. The Controller class inherits from the ControllerBase class, while the ControllerBase implementation is the IController interface.

 

ControllerBase implements the Execute method of the IController interface. After the Route matches the Controller, the Execute method is called to process the Controller. An abstract method ExecuteCore is also defined here. This method will be called at the end of the Execute method. The Controller class also defines many useful methods. The newly created Controller must inherit from this Controller class. For example, create an AdminController:

 

Next, let's talk about the important Action methods in the Controller. In aspnet mvc, URLs are mapped to an Action in the Controller, and then matched actions are used to process our business logic and return the view. All public methods in Controller are treated as Action methods. The Action method usually returns the result of an ActionResult. 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 URL in the Route that matches the Action method. For example, url: Home/Index, where Index is the Action name ).

 

ASP. net mvc provides several implementations of ActionResult.

 

Most common

The ActionResult Action method returns the result of the ActionResult type.
FileResult: Write the file content. The specific writing method is in the derived class.
PartialViewResult calls the ExecuteResult method of the parent class ViewResultBase.
Override the FindView method of the parent class.
JsonResult. The SON result is returned. Returned by the Json method.

 

Not commonly used

ViewResult. Present 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 another URL. Returned by the Redirect method.
ContentResult. Common content is returned. For example, a string. Returned by the Content method.
EmptyResult. If Action must return a null value, this result can be returned. No implementation method in Controller. You can return new EmptyResult ();

 

Parameters of the Action Method

For example, we need to define an Action method for log editing in AdminController:

 

For URL: Admin/EditPost/2, the above parameter is automatically assigned to 2. ASP. net mvc will automatically assign values to the parameters of the Action Method Based on the Route settings when matching the Route. Therefore, the prerequisite that the previous id parameter is automatically assigned to 2 is that the id parameter must be specified during Route configuration. For example:

 

  • Viewstart. cshtml is a STARTUP file when the View File is rendered and will be executed before all views (. cshtml) are executed.
  • Note that Viewstart is called only when the View is rendered. cshtml: If a View is output in PartialView (the distribution page does not call the master page) mode, _ Viewstart is not triggered. code in cshtml.
  • If a View directory (such as the Home Directory) also has a ViewStart with the same name. in the cshtm file, this _ ViewStart will also be called, but the first call is the outermost _ Viewstart, and then the ViewStart in the Home directory.

A complete View is combined with Layout and other local views. How to develop a Layout?

@ RenderBody0
When you create a view based on the layout page, the content of the view is merged with the layout page, and the content of the newly created view is
The @ RenderBody () method of the layout page is displayed between labels.

@ Html. Partial ("_ LoginPartial ")
You can call the local distribution page to display it in the current region.

@ RenderSection ("footer", false)
The layout page also has the concept of Section. That is to say, if a Section is defined in a view template, it can be presented separately.
Of course, you must define the section in the view. Otherwise, an exception occurs:

@ Section footer {
<Div> Hello word </div>
}

 

Instance product based on asp.net mvc 5.0 framework, source code: http://www.jinhusns.com/Products/Download

 

 

Related Article

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.