0

I have mvc model:

public class MyModel
{
    public int MyInt{ get; set; }
    public DateTime MyDateTime{ get; set; }

    [UIHint("_DropDown")]
    public DropDownListModel<object> MyDropDown { get; set; }

    public MyModel()
    {
        //initialize MyDropDown
    }
}

When I call @Html.EditorForModel() in view I'm just getting editors for MyInt and MyDateTime. I am able to add editor for MyDropDown using @Html.EditorFor(m => m.MyDropDown).

How can I add editor for MyDropDown to EditorForModel?

tereško
  • 58,060
  • 25
  • 98
  • 150
Ari
  • 3,101
  • 2
  • 27
  • 49
  • What's a `DropDownListModel`? Why is it using `object`? What *should* an editor for that type look like? – David Oct 13 '14 at 11:26
  • @David I've got this. It should look exactly like the one from `@Html.EditorFor(m => m.MyDropDown)`. – Ari Oct 13 '14 at 11:27
  • But what does that render? The documentation just says that `EditorForModel` will render "an HTML input" for each property on the model. That's a custom property, so there's no single HTML input to map to it. Do you have a template set up for it? What is that template's name? Does the framework otherwise find it when using other helper methods? – David Oct 13 '14 at 11:32
  • @David Yes, I have template: "_DropDown" and this template is map to this property by `[UIHint("_DropDown")]`. And using other helper method, like `EditorFor` works for this property. Why does it matter what does this template render if it works using `EditorFor`? It renders just drop down. – Ari Oct 13 '14 at 11:35
  • Just trying to know the full landscape of the framework components being used here. What happens if you use `Html.EditorFor(model => Model)` on the entire `MyModel`? What I *suspect* is that `EditorFor` (and, by extension, `EditorForModel`) doesn't examine into the non-primitive properties, at least not by default. Though I don't have an environment handy to confirm that. – David Oct 13 '14 at 11:40
  • @David `Html.EditorFor(model => Model)` works exactluy the same as `Html.EditorForModel()`. I suspect it works only for primitive too. I'm trying to find if I can use non-primitive here. – Ari Oct 13 '14 at 11:51
  • Primitives only, according to this Q&A http://stackoverflow.com/questions/7234067/uihint-not-using-editortemplate – paul Oct 13 '14 at 11:55

1 Answers1

0

I'll make this an answer because I suspect it's currently the correct approach, though as with any ASP.NET MVC answer it's likely to become incorrect in the next version...

Html.EditorForModel() is, as you may know, simply a helper with the same result as Html.EditorFor(model => Model). The current behavior of which, I suspect, is somewhat limited to primitive types on any given model. Handy for certain, but not as robust as your model seems to need.

While Html.EditorFor(m => m.MyDropDown) is smart enough to use the correct template as the editor, I don't believe Html.EditorFor(model => Model) is smart enough to use the correct template for any given property on the model, only the model itself. (There could likely be very good reasons for this, and the implementation of it may have been a trade-off between that flexibility and some problems which may have arisen from that flexibility.)

I imagine the approach required at this time is to create a custom editor template for MyModel. Internal to that template you can use Html.EditorFor(m => m.MyDropDown) which will in turn use its template. I just suspect that nested custom templating has to be manual, not automatic.

David
  • 208,112
  • 36
  • 198
  • 279