asp.net-mvc-2attributescustom-model-binderfilterattribute

ASP.NET MVC2 - Resolve Parameter Attribute in Model Binder


Given an action like:

public ActionResult DoStuff([CustomAttribute("foo")]string value) {
  // ...
}

Is there any way to resolve the instance of value's CustomAttribute within a ModelBinder? I was looking at the MVC sources and chances are I'm just doing it wrong, but when I tried to replicate their code which retrieves the BindAttribute for a complex model, calling GetAttributes() did not return the attribute I am looking for.

DefaultModelBinder

GetTypeDescriptor(controllerContext, bindingContext).GetAttributes();

Solution

  • I spoke to Brad Wilson on Twitter and he indicated that this can be achieved with the ModelMetadataProvider in the AspNet4 Futures assembly by implementing the new IMetadataAware interface on the desired types. In addition, he indicated that this functionality would be introduced in MVC 3 for both .NET 3.5 and .NET 4.0.