Bigsby
Bigsby

Reputation: 952

What is this MVC feature called?

I recall a feature in MVC that lets you influences the source object with which to derive parameter values for a given method.

public ActionResult Foo([SomethingHere] int parameter)
{
    // do something with parameter

    return View();
}

The bracketed "[SomethingHere]" would contain a member called "parameter" that MVC would then try to get an int from. I forget what this feature is called, and my Google fu is apparently weak. What is this called?

Upvotes: 4

Views: 96

Answers (2)

CodeCaster
CodeCaster

Reputation: 151690

A [Thing] is called an attribute. Attributes in and of themselves do nothing, they merely provide metadata.

Using attributes, you can instruct MVC to handle certain things different from their defaults.

In this case, you're overriding the default model binder behavior.

See for "documentation" the following: ASP.NET MVC Preview 5 and Form Posting Scenarios. Here the parameter attribute [Bind] is explained amongst others.

MSDN has even less information: BindAttribute, ModelBinderAttribute.

Upvotes: 2

Albireo
Albireo

Reputation: 11095

Are you talking about custom model binders?

public class HomeCustomDataBinder : DefaultModelBinder
{
    public override object BindModel(
        ControllerContext controllerContext,
        ModelBindingContext bindingContext)
    {
        // Various things.
    }
}

public ActionResult Index(
    [ModelBinder(typeof(HomeCustomBinder))] HomePageModels home)
{
    // Various things.
}

See ASP.NET MVC Custom Model Binder for an extended example.

Upvotes: 0

Related Questions