Andras Zoltan Andras Zoltan - 2 months ago 8
C# Question

Asp.Net MVC 2 - Bind a model's property to a different named value

Update (21st Sept 2016) - Thanks to Digbyswift for commenting that this solution still works in MVC5 also.

Update (30th April 2012) - Note to people stumbling across this question from searches etc - the accepted answer is not how I ended up doing this - but I left it accepted because it might have worked in some cases. My own answer contains the final solution I used, which is reusable and will apply to any project.

It's also confirmed to work in v3 and v4 of the MVC framework.

I have the following model type (the names of the class and its properties have been changed to protect their identities):

public class MyExampleModel
{
public string[] LongPropertyName { get; set; }
}


This property is then bound to a bunch (>150) of check boxes, where each one's input name is of course
LongPropertyName
.

The form submits to url with an HTTP GET, and say the user selects three of those checkboxes - the url will have the query string
?LongPropertyName=a&LongPropertyName=b&LongPropertyName=c


Big problem then is that if I select all (or even just over half!) the checkboxes, I exceed the maximum query string length enforced by the request filter on IIS!

I do not want to extend that - so I want a way to trim down this query string (I know I can just switch to a POST - but even so I still want to minimize the amount of fluff in the data sent by the client).

What I want to do is have the
LongPropertyName
bound to simply 'L' so the query string would become
?L=a&L=b&L=c
but without changing the property name in code.

The type in question already has a custom model binder (deriving from DefaultModelBinder), but it's attached to its base class - so I don't want to put code in there for a derived class. All the property binding is currently performed by the standard DefaultModelBinder logic, which I know uses TypeDescriptors and Property Descriptors etc from System.ComponentModel.

I was kinda hoping that there might be an attribute I could apply to the property to make this work - is there? Or should I be looking at implementing
ICustomTypeDescriptor
?

Answer

You can use the BindAttribute to accomplish this.

public ActionResult Submit([Bind(Prefix = "L")] string[] longPropertyName) {

}

Update

Since the 'longPropertyName' parameter is part of the model object, and not an independent parameter of the controller action, you have a couple of other choices.

You could keep the model and the property as independent parameters to your action and then manually merge the data together in the action method.

public ActionResult Submit(MyModel myModel, [Bind(Prefix = "L")] string[] longPropertyName) {
    if(myModel != null) {
        myModel.LongPropertyName = longPropertyName;
    }
}

Another option would be implementing a custom Model Binder that performs the parameter value assignment (as above) manually, but that is most likely overkill. Here's an example of one, if you're interested: Flags Enumeration Model Binder.