ASPNET MVC: Why This Action Method Binding Convention?

Actually, it makes perfect sense. The index is into the collection, not the property. You don't have a collection of PropertyName, you have a Collection of CollectionName that has a PropertyName.

Up vote 2 down vote favorite 1 share g+ share fb share tw.

Having just spent two amazingly frustrating hours formatting some hidden fields to bind properly to action method parameters -- the second time I've had this experience in the last week -- I'm now very curious as to why the MVC architects chose the particular binding convention that they did for lists (and dictionaries) of objects. Here's my painfully gathered understanding of the format expected by the default binding engine. In my example I want to bind to a List, where the CustomClass type exposes a public property called PropertyName: prefix is the ViewDataDictionary.TemplateInfo.

HtmlPrefix, if one has been defined. I find it deeply counter-intuitive to start the reference to something with the indexing information (i.e. , the idx piece).

I also find it disturbing that nowhere in this construct do I make reference to the name of the action method parameter to which I'm binding. That seems to be in direct contrast to what happens with, say, a primitive model property: public ActionResult SomeActionMethod( string Text )... I understand I can roll my own model binder in MVC. That doesn't seem like a profitable use of my time, although spending hours trying to puzzle out the correct format for hidden fields isn't profitable either :), so maybe I'll try that.

I also know that I can let MVC do all the heavy lifting by creating a type template using @Html.HiddenFor(), and then outputting instances of CustomClass though a simple partial view that has CustomClass as a model and the single line @Html.DisplayForModel(). But that seems like going the long way around the barn, too. Besides, there are limitations to using the @Html.

Hidden helpers (e.g. , they "helpfully" raid the cache after postback to fill in values, so writing @Html. Hidden("fieldname", value) doesn't guarantee value will end up being output -- you may get the older value in the cache instead, which was another hour-long annoyance today). But I'm mostly just curious why this format was chosen.

Why not something more C/C++/C#/VB like: Edit: Good point on where I put the index parameter in my example. You're right, the property isn't indexed, the containing class is. However, that doesn't change the basic situation.

Why isn't the syntax something like: The standard syntax ignores the parameter name with collections of custom types, and "guesses" the custom type from the property names. That's bizarre...so there must be a story or choice behind it :). Asp.

Net-mvc architecture link|improve this question edited Apr 1 at 15:58 asked Apr 1 at 4:23Mark292210 82% accept rate.

Sorry if this is a dumb question, but can you clarify what you mean by "ViewDataDictionary.TemplateInfo. HtmlPrefix"? – dbaseman Apr 1 at 4:42 @dbaseman - HtmlPrefix is what allows templates to properly format the name fields when they are bound in a collection.

– Mystere Man Apr 1 at 4:47.

Actually, it makes perfect sense. The index is into the collection, not the property. You don't have a collection of PropertyName, you have a Collection of CollectionName that has a PropertyName.

To put this another way: public class Foo { public string Bar { get; set; } } var foos = List(); for (var I = 0; I When the model binder deserializes the post values, it has to know which collection to insert the values into, and it has to know what index each item is for. So it has to know to create a Collection of Foos, of x number of items, and which indexes each Bar is associated with.

This is described in detail haacked.com/archive/2008/10/23/model-bin... – Erik Philips Apr 1 at 7:36 Thanks for the link, Erik. As I tried to explain in my edit I don't think the article answers my basic question, though. I wonder if the standard binding isn't an artifact of how the binder treats the model class?

By that I mean the binder doesn't look for: when binding. It cuts down on required verbosity by assuming the target type is whatever the Model is. Perhaps the MVC team simply "recycled" the model class binder rather than create another one.

– Mark Apr 1 at 16:00.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions