views:

33

answers:

1

Instead of using DisplayFor and EditorFor, I would like to create a more generic ContentFor. In that Html extension it would take into account Metadata values to determine how to render the resulting control. The only piece of the puzzle I am not am to determine is this: Is there a way to determine if I am currently rendering a DisplayTemplate or an EditorTemplate. As a real-world example of this, when rendering a string, for the display version I would like to render it as a , but when rendering the editor version, I would want to render it as a text box.

To better explain, let's say I have two templates called Address.ascx, one in the DisplayTemplates directory and one in the EditorTemplates directory. I would like both of them to use ContentFor to render, but in the display version it renders as a label and in the editor version it renders as a textbox.

A: 

Using two ASCX files to call a single file control (which is doable, just do another RenderPartial or DisplayFor/LabelFor) doesn't make sense to me. It breaks the "seperation of concerns". Label displays labels, and Display displays values, it doesn't make sense for a control to try and figure out what way you want it to display.

If you want a use a custom display or label for a property, use the UIHint data Annotation.

[UIHint("MyCustomControlName")]

Then in the DisplayTemplates and EditorTempaltes create a "MyCustomControlName.ascx" file to display that property however you want. Additionally, the ascx controls can read custom Model Metadata and do whatever it is you need done. Example at http://weblogs.asp.net/seanmcalinden/archive/2010/06/11/custom-asp-net-mvc-2-modelmetadataprovider-for-using-custom-view-model-attributes.aspx.

Erik Philips