views:

43

answers:

1

I've looked at most of the ModelBinding examples but can't seem to glean what I'm looking for.

I'd like:

<%= Html.TextBox("User.FirstName") %>
<%= Html.TextBox("User.LastName") %>

to bind to this method on post

public ActionResult Index(UserInputModel input) {}

where UserInputModel is

public class UserInputModel {
    public string FirstName {get; set;}
    public string LastName {get; set;}
}

The convention is to use the class name sans "InputModel", but I'd like to not have to specify this each time with the BindAttribute, ie:

public ActionResult Index([Bind(Prefix="User")]UserInputModel input) {}

I've tried overriding the DefaultModelBinder but can't seem to find the proper place to inject this tiny bit of functionality.

+1  A: 

The BindAttribute can be used at the class level to avoid duplicating it for each instance of the UserInputModel parameter.

======EDIT======

Just dropping the prefix from your form or using the BindAttribute on the view model would be the easiest option, but an alternative would be to register a custom model binder for the UserInputModel type and explicitly looking for the prefix you want.

Derek Greer
That's good to know, and makes my task more manageable. Though I'd still like to know if this can be done behind the scenes in the model binding process since the attribute requires constant values.
Mitch R.
Maybe my original question wasn't clear. I'd like to know HOW to derive from the DefaultModelBinder and add this extra prefix check there based on the model type.
Mitch R.