Note that there are some explanatory texts on larger screens.

plurals
  1. POAsp.Net MVC 2 - Bind a model's property to a different named value
    primarykey
    data
    text
    <p><strong><em>Update (21st Sept 2016)</em></strong> <em>- Thanks to Digbyswift for commenting that this solution still works in MVC5 also.</em></p> <p><strong><em>Update (30th April 2012)</em></strong> <em>- 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. <a href="https://stackoverflow.com/a/4316327/157701">My own answer contains the final solution I used</a>, which is reusable and will apply to any project.</em></p> <p><em>It's also confirmed to work in v3 and v4 of the MVC framework.</em></p> <p>I have the following model type (the names of the class and its properties have been changed to protect their identities):</p> <pre><code>public class MyExampleModel { public string[] LongPropertyName { get; set; } } </code></pre> <p>This property is then bound to a bunch (>150) of check boxes, where each one's input name is of course <code>LongPropertyName</code>.</p> <p>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 <code>?LongPropertyName=a&amp;LongPropertyName=b&amp;LongPropertyName=c</code></p> <p>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!</p> <p>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).</p> <p>What I want to do is have the <code>LongPropertyName</code> bound to simply 'L' so the query string would become <code>?L=a&amp;L=b&amp;L=c</code> but <strong><em>without changing the property name in code</em></strong>.</p> <p>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.</p> <p>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 <code>ICustomTypeDescriptor</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload