DISQUS

DISQUS Hello! Emad Ibrahim is using DISQUS, a powerful comment system, to manage its comments. Learn more.

Community Page

Jump to original thread »
Author

Use ModelBinder to Generically Bind Complex Types

Started by eibrahim · 10 months ago

ASP.NET MVC Preview 5 introduce the ModelBinder attribute that can be used to decorate a complex type in an Action.  This allows us to have actions that look like this

public ActionResult Create([ModelBinder(typeof(GenericBinder))] ContactList myList)

  ... Continue reading »

6 comments

  • Isn't this what the ComplexBinder that comes with MVC P5 already does?
  • You are absolutely right. It is hard to find these things without good
    documentation. But I just found some info at
    http://weblogs.asp.net/scottgu/archive/2008/09/...
    I will update the post to direct readers there.

    Thanks for the heads up.
  • No problem :)
    btw: this threaded comment view is awesome
  • Yeah, I love Disqus... I actually don't have to log in to my admin tool to
    moderate my comments. I simply reply to a comment with "approve" and it
    gets approved. Or If I reply with some other text, it approves the comment
    and submits my text as a comment. Very neat.
    I am looking into the ComplexModelBinder and trying to get things working
    with the built-in stuff :)
  • The ComplexModelBinder doesn't make much sense in his post.

    How do you use it? Why the need to 'register' it.

    Seems to me if you pass a complex type as a parameter, it would map the form variables to the type with reflection - and only if you need to have more complex mapping would you create a specific binder...
  • Well how about this:

    I exposed the protected UpdateModel in my controller:

    public void UpdateModel(BusinessSavingsRequest bsr, string[] keys)
    {
    base.UpdateModel(bsr, keys);
    }

    and then call it from my binder:

    public object GetValue(ControllerContext controllerContext, string modelName, Type modelType, ModelStateDictionary modelState)
    {
    BusinessSavingsRequest bsr = new BusinessSavingsRequest();
    string[] properties = typeof(BusinessSavingsRequest).GetProperties().Select(p => p.Name).ToArray();
    ((MvcApplication.Controllers.BusinessSavingsController)controllerContext.Controller).UpdateModel(bsr, properties);
    return bsr;
    }

    Perhaps if this is all it needed to do I wouldnt have a custom binder for the type.

Add New Comment

Returning? Login