views:

242

answers:

2

Hi

How does binding work? Like how many fields have to match up to make a successful bind. Say you got a Product class with 5 fields and only 4 of the fields match up does it still bind?

Also I know they have an exclude for binding but how do you do multiple excludes? Like if I have 2 fields I want to exclude how do you write that?

+1  A: 

To exclude any number of fields from binding just list them in the action's bind attribute:

public ActionResult Edit([Bind(Exclude = "Id, Username")] int id, FormCollection collection)

At the same time you can explicite define which fields to update:

TryUpdateModel(user.Person, new string[] { "firstname", "lastname", "email", "phone" });
twk
A: 

You could also just type the view

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<Person>" %>

And then

[AcceptVerbs(HttpVerbs.Post)]    
public ActionResult Edit([Bind(Exclude = "Id, Username")]Person person)
{
  // Do the logic.
}

Instead of using TryUpdateModel

Patrik Potocki