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.
First things first
@EditoFormodel()in your page looks bizarre. I guess you meant something else.Now to the point: you have decorated the
Charitiesproperty withUIHint. This property is of typeSelectList. This means that theCharities.cshtmltemplate should be strongly typed toSelectList, not toMyModelas you did. You could simply remove this UIHint attribute and have the following view model:and in your view:
and then inside
~/Views/Shared/EditorTemplates/MyModel.cshtmlhave:That's the standard conventions. If you want your editor template to be called
Charities.cshtmlyou could do this in your page:But usually you would have the following model:
Now if your main view is strongly typed to
FooViewModelyou can:which will render the
Charities.cshtmleditor template.