Points to Remember while Binding in ASP.NET MVC framework


Before knowing the steps. the user should understand what Binding actually means as it is a simple to do and also know as Model Binding in ASP.NET MVC framework. The purpose of Binding is to take data from database and store data into database or in other words we can say Binding helps in retrieving the information and storing information.

It is done with the help of HTTP that carries the data you need  which can be performed by DefaultModelBinder

Now here are the following points that should be followed while Binding in ASP.NET MVC Application

1. Binding Should be preferred over Request.Form

That is in case you are writing like this :

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create()
{
Recipe recipe = new Recipe();
 recipe.Name = Request.Form["Name"];
 //
return View();
}

If you use like the above code that will be wrong because the Model Binder can restrict you from Request and HttpContext
properties.

Another way will be to use FormCollection parameter

public
ActionResult Create(FormCollection values)
{
Recipe
recipe = new Recipe();
recipe.Name = values["Name"];     
//
return View();
}

In this way user don't need to dig into the Request object but the ultimate way will be if your data is in Request.Form that should be like this

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Recipe newRecipe)
{           
//
return View();
}

As this will bind the object automatically

2. Second point to remember is to use Custom Model Binder

Extensibility point in the MVC framework is Model Binding that means we can customize the binding according to our use in place of Default Binding for that purpose user need to implement the IModeBinder Interface.

public interface IModelBinder
{
object BindModel(ControllerContext controllerContext,
ModelBindingContext
bindingContext);
}

3. Now third point that should be remember while binding is Custom Model Binding via Inheritance

As in the second point we got to know about  Custom Model Binder now in this point in which Inheritance is highlighted that will help the user to cut down the required amount of work by inheriting the DefaultModelBinder and can add some custom, logic that are required by the user.

4. As while Binding user should use some validation i.e Data Annotations for Validation

These types of  validation that are applied while Binding are generally used for server-side validation by simply manipulating your model with attributes .

public class
Recipe
{
[Required(ErrorMessage="We need a name for this dish.")]
[RegularExpression("^Bacon")]
public string Name { get; set; }
//


5. Fifth point to remember is recognize Binding and Validation as two phase

As binding is to take data from database and store data into database and on the other hand validation is use for checking  the application but if you want to perform validation along with binding together in a MVC Model Binder that will be same as DataAnnotationsModelBinder will work


 Cc301328.asp0112fig03(en-us.gif

For simple property validation then user can override the OnPerpertyValidating method. 

6. And the last point to remember is Binding is all about the Environment

As Model Binder helps in separating the code from the request and its associated environment and binder which means to move the data from the routing data into the model.In this case there is no restriction of finding the required data for the model.

In Conclusion

While Binding in ASP.NET MVC Application keep these points in mind and take the benefits  and that will enhance your Binding.


Similar Articles