
This is the third article in a series of articles about the MVC Framework. In the
first one I tried to answer the question “Why do we need the MVC Framework?”, And in the
second I began a story about how it works. The story in the previous article meant a superficial description of the entire process of the MVC Framework, without going into details. Details will follow later in the series of articles. This article will continue the story about the internal mechanism of the MVC Framework, which was interrupted by the description of Model Binding. C it will begin.
Model binding
When the MVC Framework mechanism has defined a controller class and an action method in it that should participate in forming a response to a client request, the task is to transfer the request parameters to the action. In the simplest case, the data transfer is performed by comparing the request parameters with the method parameters. For example, if a user submits a form with data through filling in certain html elements, then the values of the contents of these elements will be passed to the action through the method parameter of the same name. The
textarea data with the
name attribute equal to the
address will be matched with the action method parameter defined as the
string address .
Model Binding deals with this mapping, but its capabilities do not end with such a simple example. What to do when there are several dozens of parameters on the form? We will not begin to create methods with two tens parameters, only to obtain data from the form. Indeed, this is not necessary. Model Binding provides a great opportunity to redefine the order of matching parameters and make initialization not in the form of “form parameter = method parameter”, but more complicated. For example, we can define a class or structure that will contain the form data and in the method define only one parameter as an instance of this structure. Then, after defining the Model Binding mechanism for our complex type, we can initialize its properties with request parameters and call an action method, passing it an initialized object. As a result, numerous parameters on the form will be passed to the action method as an instance of a class with filled properties. Very comfortably.
But the MVC Framework developer does not even need to do this, because the MVC Framework implements the
DefaultModelBinder class, which does all the work of matching query parameters to complex types. However, this does not prevent you from defining your own Model Binding mechanism for certain types of parameters. This can be useful when, in addition to simply transferring data from a request, you want to perform some more actions with an instance of a complex type.
')
The MVC Framework application has a standard
ModelBinders property that contains a collection of user-defined parameter mapping mechanisms. These mechanisms are an implementation of the
IModelBinder interface, which contains only one
BindModel method with the following definition:
public object BindModel (ControllerContext controllerContext, ModelBindingContext bindingContext);Using the controllerContext parameter passed by the MVC Framework, the developer can access all the data of the request context, including the parameters. The MVC Framework mechanism expects an object from the implementation of the IModelBinder interface to determine the result of matching query parameters for a particular complex type to which the attribute is applied. Registration of your Model Binding mechanism is performed in
global.asax in the
Application_Start method as follows:
ModelBinders.Binders.Add (typeof (UserData), new UserDataBinder ());where,
typeof (UserData) is an indication for which complex type a matching mechanism is created,
new UserDataBinder () is an instance of a class implementing IModelBinder.
Note. In order to use these mechanisms not globally, but only locally, one-time, the developer, instead of registering a global Model Binding mechanism, can mark the required action parameter with the ModelBinder attribute, which contains the parameter defining the type of mechanism. How this is done is shown below:public ActionResult Update ([ModelBinder (typeof (UserDataBinder))] UserData userData);When the MVC Framework calls an action method, for each parameter of a complex type, it looks for the Model Binding mechanism in the ModelBinders collection and calls it if it exists. After that, the custom implementation of the parameter mapping forms an object that is cast to the type of the parameter and transmitted when the action is called. In the case when there is no custom implementation for the complex type, the default parameter matching mechanism comes into play, which is implemented via DefaultModelBinder.
The developer can even more flexibly customize the execution of parameter mappings using the
BindAttribute attribute, which allows you to define several mapping rules:
- the prefix used in the markup for the default mapping;
- white and black lists of parameter names.
The work of this attribute will be discussed in more detail in the following articles.After all the parameters are matched, the
ActionInvoker mechanism calls an action method that executes the action logic and, according to the rules of the MVC Framework, must return the result as an
ActionResult or its derivatives.
Execution of the result and ActionResult
So, the action was called and returned the result in the form of an ActionResult or its derivatives. In general, the MVC Framework mechanism contains a number of such derived class
ActionResult: ViewResult, JsonResult, FileResult, RedirectResult, RedirectToRouteResult, ContentResult, EmptyResult . Already by name you can determine the purpose of each of them. Each of these types of action result reflects the result that will be returned to the client in response to the request. It is important to note that the action does not write to the result stream, generate markup or other actions to send the result. All actions of the MVC Framework return data in the form of a specific ActionResult mechanism, which in turn does everything necessary so that the client receives the data in the desired form. This is called the execution of the result.
In more detail about the work of ActionResult and the purpose of all its derivatives will be covered in subsequent articles.The most powerful feature of the MVC Framework is that the developer can define his own version of the implementation of ActionResult to generate his own result in response to a client request. So a variant of ActionResult, which returns an RSS feed, can be implemented very easily.
After receiving the result from the action in the form of ActionResult, the MVC Framework (represented by ActionInvoker) calls the ExecuteResult method, which performs all the work on creating markup, formatting data, creating http headers and other work. This completes the work of the MVC Framework. Together with him ends the article describing the mechanism of the MVC Framework.
