Understand. net mvc implementation principle ActionResult/View

Source: Internet
Author: User

The previous article learned about the Action process from the request to the Controller. This article continues to look at the process where the source code processing Action receives the request data and returns the ActionResult to the View.
Problems to be discussed in this section
Parameter passing process of Action
ActionResult
IView/IViewEngine/ViewEngineCollection/ViewEngineResult
I remember from the decompilation source in the previous article that the execution sequence of the Filter mentioned the variable named 1, 2, and 3. In the source code of MVC3, Microsoft changed it.
AuthorizationContext authContext = InvokeAuthorizationFilters (controllerContext, filterInfo. authorizationFilters, actionDescriptor); ActionExecutedContext postActionContext = InvokeActionMethodWithFilters (controllerContext, filterInfo. actionFilters, actionDescriptor, parameters); ExceptionContext exceptionContext = InvokeExceptionFilters (controllerContext, filterInfo. predictionfilters, ex );

1. parameter passing process of Action
When defining the Action method, we often use the following parameters:
View Code
Using System; using System. collections. generic; using System. linq; using System. text; using System. web; using System. web. mvc; using System. componentModel; using Demo. model; using Demo. service. IService; namespace Demo. mvc. controller {[HandleError] public class MenuController: Core. baseController {private IMenuService service; public MenuController (IMenuService service) {this. service = service;} public Acti OnResult Index () {return View ();} [ActionName ("ShowId")] public ActionResult ShowId (string id) {return View (service. get (id);} // failed UpdateModel will throw an exception or TryUpdateModel will try to convert NULL fields such as time integers public ActionResult SaveModel (string name, string age) {var permisson = new INFRA_MENU_PERMISSION (); // UpdateModel <INFRA_MENU_PERMISSION> (permisson); TryUpdateModel (permisson); ViewData. model = permisson; retu Rn View ();} // FormCollection method // You can bind any collection type: IList <Book>, ICollection <Book>, IEnumerable <Book>, List <Book>, book [] dictionary public ActionResult SaveModel (FormCollection formCollection) {INFRA_MENU_PERMISSION permisson = new INFRA_MENU_PERMISSION (); permisson. ACTION_NAME = formCollection ["ACTION_NAME"]; permisson. CREATETIME = Convert. toDateTime (formCollection ["ACTION_NAME"]); service. update (permisson); Return RedirectToAction ("Index");} // IModelBinder DefaultModelBinder binds the Request passing parameters to the [OutputCache] public ActionResult SaveModel (INFRA_MENU_PERMISSION permisson) {service. update (permisson); return RedirectToAction ("Index");} // attributes not bound to public ActionResult SaveModelWithExclude ([Bind (Exclude = "CONTROLLER_NAME")] INFRA_MENU_PERMISSION permisson) {service. update (permisson); return RedirectToAct Ion ("Index");} // attributes bound to public ActionResult SaveModelWithInclude ([Bind (Include = "CONTROLLER_NAME")] INFRA_MENU_PERMISSION permisson) {service. update (permisson); return RedirectToAction ("Index") ;}} Action supports parameter type binding, Dictionary keys, objects, sets, and so on. In addition to the default binding method, you can inherit IModelBinder to override defamodelmodelbinder.
You can implement a ModelBinder code and process the bound Attribute (For details, refer to the code comment) by yourself. In this way, you can solve the value binding problem of some complex objects.
View Code
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; using System. componentModel; namespace Demo. mvc. exModelBinder {// implementation similar to DefaultModelBinder refer to defamodelmodelbinder source code to rewrite the source code of ModelBinder // DefaultModelBinder in MVC2 and MVC3 is different, mainly because the ValueProvider field MVC2 is a dictionary set, MVC3 is a policy mode public class VOModelBinder: IModelBinder {# region IModelBinder member public object BindModel (ControllerContext controllerContext, ModelBindingContext bindingContext) {object model = Activator. createInstance (bindingContext. modelType); PropertyDescriptorCollection col = TypeDescriptor. getProperties (model); foreach (PropertyDescriptor item in col) {// ValueProvider records the Request parameter key value pair var result = bindingContext. valueProvider. getValue (item. name); if (result! = Null) {var value = result. convertize (item. PropertyType); item. SetValue (model, value); & nbs

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.