2

I have a model like this:

public class MyModel {
    [ScaffoldColumn(false)]
    public int CharityId { get; set; }

    [UIHint("Charities")]
    public SelectList Charities { get; set; }
}

Then I have an EditorTemplate called Charities.cshtml:

@model MyModel

@Html.DropDownListFor(model => model.CharityId, Model.Charities)

Then in my page:

@model MyModel

@Html.EditorForModel()

However, no matter what, it doesn't render the Charities template. I've been wracking my brain on this, and it should work.. but it's not.

Any ideas?

EDIT:

The issue is that the default Object template will not render a complex object other than itself (so called shallow dive), even if there is a UIHint. I was under the impression that a UIHint would make it render the template, but I was apparently wrong.

The fix is to not try to let the model render itself. That is sad.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291

1 Answers1

5

First things first @EditoFormodel() in your page looks bizarre. I guess you meant something else.

Now to the point: you have decorated the Charities property with UIHint. This property is of type SelectList. This means that the Charities.cshtml template should be strongly typed to SelectList, not to MyModel as you did. You could simply remove this UIHint attribute and have the following view model:

public class MyModel {
    [ScaffoldColumn(false)]
    public int CharityId { get; set; }

    public SelectList Charities { get; set; }
}

and in your view:

@model MyModel
@Html.EditorForModel()

and then inside ~/Views/Shared/EditorTemplates/MyModel.cshtml have:

@model MyModel
@Html.DropDownListFor(model => model.CharityId, Model.Charities)

That's the standard conventions. If you want your editor template to be called Charities.cshtml you could do this in your page:

@model MyModel
@Html.EditorForModel("Charities")

But usually you would have the following model:

public class FooViewModel
{
    [UIHint("Charities")]
    public MyModel Charities { get; set; }
}

Now if your main view is strongly typed to FooViewModel you can:

@model FooViewModel
@Html.EditorFor(x => x.Charities)

which will render the Charities.cshtml editor template.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • No, I do not h ave a MyModel.cshtml. I'm using the default Object template to render that. I've corrected my mistyping for EditorForModel. I have also tried to strongly type Charities.cshtml to SelectList and that did not render either. I would think if the model type was wrong, it would simply throw. But it's not even choosing Charities.cshtml to render in the first place. – Erik Funkenbusch Aug 29 '11 at 20:30
  • @Mystere Man, as I said the problem with your code is that you have specified a UIHint attribute on the Charities property which is of type SelectList whereas your Charities.cshtml is strongly typed to MyModel which is inconsistent. Also you should use `Html.EditorFor(x => x.Charities)` if you strongly type your Charities.cshtml view model to SelectList if you want it to render. – Darin Dimitrov Aug 29 '11 at 20:33
  • If that were the case, it should throw, not fail to render. In any case, I tried strongly typing it to SelectList and it still did not try to render, verified by setting a breakpoint in the view. It's as if it's completely ignoring the UIHint, or something is overriding it. – Erik Funkenbusch Aug 29 '11 at 20:35
  • @Mystere Man, did you replace `@Html.EditorForModel()` with `@Html.EditorFor(x => x.Charities)`? If it still doesn't render I suspect that you have placed this `Charities.cshtml` file in some wrong folder. – Darin Dimitrov Aug 29 '11 at 20:37
  • I'm trying to let the model render itself, if at all possible. It seems that the default Object template will not render a complex object even with a UIHint. – Erik Funkenbusch Aug 29 '11 at 20:39
  • @Mystere Man, no it will not. The object template doesn't recurse into complex sub objects as explained by Brad Wilson here: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html. It explains how you could tweak the default object template to make it work. – Darin Dimitrov Aug 29 '11 at 20:40