Asp. Net MVC model binding

Source: Internet
Author: User

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
{

}
}

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.