Asp. net MVC has two built-in model binding functions. The first is to use UpdateModel () to update the attributes of an existing model object, and the second is to pass the model object, as a parameter of the action method. These two types of binding functions are quite powerful. As follows:
First model binding
[AcceptVerbs (HttpVerbs. Post)]
Public ActionResult Edit (int id, FormCollection formValues)
{
Dinners dinners = dinnerRepository. GetDinners (id );
Try
{
UpdateModel (dinners );
DinnerRepository. Save ();
Return RedirectToAction ("Details", new {id = dinners. DinnerID });
}
Catch
{
ModelState. AddRuleViolations (dinners. GetRuleViolations ());
Return View (dinners );
}
}
Model binding
[AcceptVerbs (HttpVerbs. Post)]
Public ActionResult Create (Dinners dinners)
{
If (ModelState. IsValid)
{
Try
{
Dinners. HostedBy = "SomeUser ";
DinnerRepository. Add (dinners );
DinnerRepository. Save ();
Return RedirectToAction ("Details", new {id = dinners. DinnerID });
}
Catch
{
ModelState. AddRuleViolations (dinners. GetRuleViolations ());
}
}
Return View (dinners );
}
By default, the UpdateModel () method tries to update all attributes based on the matching form parameter values. Similarly, the model object passed as a parameter to the action method sets the attributes of all Model Objects Based on form parameters. However, sometimes we can update some attributes of an explicitly specified model object, which is called the Lock of model binding, you can lock the binding Based on purpose or type.
Lock the binding Based on purpose. The following describes the locking method for the first model binding:
[AcceptVerbs (HttpVerbs. Post)]
Public ActionResult Edit (int id, FormCollection formValues)
{
Dinners dinners = dinnerRepository. GetDinners (id );
// Specify the updatable attribute list of Model Objects
String [] allowPropery = new [] {"Title", "Description", "ContactPhone "};
Try
{
// Use UpdateModel to overload the Method
UpdateModel (dinners, allowPropery );
DinnerRepository. Save ();
Return RedirectToAction ("Details", new {id = dinners. DinnerID });
}
Catch
{
ModelState. AddRuleViolations (dinners. GetRuleViolations ());
Return View (dinners );
}
}
The second method of locking model binding:
[AcceptVerbs (HttpVerbs. Post)]
Public ActionResult Create ([Bind (Include = "Title, Description, ContactPhone")] Dinners dinners)
{
If (ModelState. IsValid)
{
Try
{
Dinners. HostedBy = "SomeUser ";
DinnerRepository. Add (dinners );
DinnerRepository. Save ();
Return RedirectToAction ("Details", new {id = dinners. DinnerID });
}
Catch
{
ModelState. AddRuleViolations (dinners. GetRuleViolations ());
}
}
Return View (dinners );
}
Lock binding based on type
Lock the binding Based on the type. Once a binding rule is set, it can be used in all controllers and actions, including parameters of the UpdateModel method and Action method.
Add the [Bind] attribute to the class or Global in the application. in the asax file (useful if the type is not customized), to customize the type binding rules. Use the Include and Exclude attributes of the Bind attribute to specify the attributes in the class or interface that can be bound.
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Data. Linq;
Namespace NerdDinner. Models
{
[System. Web. Mvc. Bind (Include = "Title, Description, ContactPhone")]
Public partial class Dinners
{
}
}