4

I'm aware that you can do this:

public ActionResult DoSomething([Bind(Exclude = "CreationDate")] Item item)
{ /*...*/ }

However, I found that you can also attach that Bind attribute on top of the method, e.g.

[Bind(Exclude = "CreationDate")] 
public ActionResult DoSomething(Item item)
{ /*...*/ }

Does this have the exact same effect? What if you bind to more than one parameter?

3
  • 3
    I can't get the second example ([Bind] on the method itself) to compile. Are you sure you're using System.Web.Mvc.BindAttribute instead of another custom attribute called [Bind] with looser usage restrictions? Commented Sep 14, 2009 at 18:19
  • 4
    Bind attribute just work on the class and parameter declarations Commented Feb 18, 2010 at 14:44
  • It looks like you can apply the Bind attribute to individual parameters. For example, if you had a view that included two instances of Person, one prefixed with "Manager" and the other with "Lackey", you could use something like: public ActionResult Flog([Bind(Prefix="Manager")] Person manager, [Bind(Prefix="Lackey")] Person designatedVictim) Commented Oct 27, 2011 at 19:37

1 Answer 1

6

Note that the Bind attribute is no longer applicable to functions; it is only valid attached to a parameter or class. When attempting to utilize the attribute on a method, an error presents: Attribute 'Bind' is not valid on this declaration type. It is valid on 'class, parameter' declarations.


The second form is the one that I have seen more commonly. To my knowledge, both forms do exactly the same thing.

The Exclude option takes a comma-separated list of attributes. The Exclude list simply excludes one or more parameters from binding; it has no effect on the remaining parameters.

Example:

[Bind(Exclude="ID, Name")]
Sign up to request clarification or add additional context in comments.

1 Comment

The second form won't compile. The Bind attribute's allowed targets are class and parameters. BindAttribute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.