How does EditorFor work?

Oct 26, 2012 at 11:30 PM

I am hoping someone will help me understand the source code for EditorFor so I can implement something similar.  I'll describe my use case first.

I want to create a helper method that allows someone to specify a property using an expression.

Essentially I have a partial view that allows someone to select items from a list.  In the parent view, the person consuming this partial, I want them to be able to do something like

@Html.OrdersList(m=>m.SelectedOrders, "ActionNameProvidingList")


The expression return type, SelectedOrders, would need to be of a specific strongly typed OrdersCollection.  The purpose of this is to allow the OrdersList partial to generate hidden fields named appropriately so they bind to the parent View's model on post.  (Since complex objects don't post well, OrdersCollection would actually have a SelectedIds property which would be bound to a hidden field.)  The point being, someone could display multiple of these on a page mapped to different properties on their own view.  Using RenderPartial this would be more difficult because in addition to passing in the list of selected items, you'd need to pass a string which is the name of the property that it will bind to.  Somewhat less user friendly for the consumer of the component.  Since the field providing the list of selected items during a GET will be the same field it should bind to on the Post, they will always be the same name, just like the scenario for EditorFor.

I could create a Editor template for the collection, but I want to have some other strongly typed parameters as well outside of the model, so OrderFor helper method is more appealing.

Getting to my question:

When I look at EditorFor, it calls upon TemplateFor in TemplateHelpers.cs, which seems to call a different overload of itself, but when I look at the other overload, it is commented like this:

// Unit testing version

 internal static string TemplateFor

In addition to being commented as unit testing(which implies to me it is not what is used at runtime), it also calls a function I can't find "templateHelper(...", because none of the other functions named like that start with a lower case 't'.  So I get stuck there since I don't know where this lowercase 't' templateHelper method is at.  Can someone get me on the right track?

Maybe also explain briefly the architecture?  It appears as though the overloads that return MvcHtmlString call overloads that just return string, which in turn is passed to a Create method that I assume does some magic to turn the string into a MvcHtmlString (I haven't gotten as far as looking at that yet).

So far though, all I've seen is lots of one line methods where they call upon some other method/overload.  I haven't been able to trace all the way through to where I actually see some meat+potatos code doing things like if(expression is string) { generate html for textbox }, that would make me feel like I understand the magic behind these methods and how I can emulate that pattern for more specialized helpers.

NOTE: Some might say that I'm better served using some other feature of MVC to accomplish my goal, which might be valid, but regardless I'd really like to understand how these *For helper methods work so I can evaluate that for myself in the future.