In the previous two articles, we studied the vieweingine improvements of the ASP.net Mvc Preview5, and starting with this article, we began to look at the new features in PREVIEW5: Modelbinder.
First, let's take a look at what is the Modelbinder feature, and what's the use of it, in previous versions, if we were going to get the data in action, there were generally three ways, one through the arguments to the action, but for some reason there was only a simple type of parameter, such as int, string and so on, the second way is to get it through the request, but when there is more data, this way is very difficult. The third is to use the extension method in Bindinghelperextensions, but this way still cannot satisfy our request. Our ideal way is :
If you can write this, you will be able to get the incoming object directly in the parameters. It will make the structure of the program more reasonable. In the new edition, the framework introduces Modelbinder to solve this problem, so that our ideal effect can be achieved.
The role of Modlbinder is to create a conversion relationship between HTML parameters and objects, through the system built-in or user-added binder to complete the conversion process, whenever the action contains parameters, the system will automatically query the URL or whether the form contains the corresponding values, A certain Modelbinder object is then invoked to convert the parameter to the Passed-in value.
Below we analyze the work process of the modelbinder. We know that the execution of the action is actually controlleractioninvoker, and there are some methods in this class that, in the process of execution, It first finds the method that corresponds to the action in controller, and then determines whether methods have parameters and, if there are parameters, assigns them to them in a particular way. This part of the specific code we'll take a look at: