I'm using ASP.NET MVC 4.5+, and I'm trying to understand why one would want to render a partial view utilizing Html.EditorFor rather than Html.PartialView.
What I've found is that EditorFor "respects the model hierarchy", which I've gather to mean that, for an input in a view rendered by EditorFor, input names/ids reflect the nested levels of the calling model, and that PartialViews don't do this.
However, in the following partial view:
@model someModel
...
@Html.TextboxFor(m => m.complexObject.property)
...
will render the textbox as
<input id="complexObject_property" name="complexObject.property" ... >
when rendered via Html.PartialView, at least in MVC 4.5+. Which seems to me to be respecting the model hierarchy pretty well.
I understand that DisplayFor and EditorFor will tell the framework to automagically look into the ~/*Templates folder to return views for views called by these controls. So, the only thing I can think of at the moment is that we'd use Display/EditorFor to allow for this sort of file/folder structure & automagic rendering that is a bit more semantic than looking in a "Shared" folder for a specifically named partial.
As it stands now, even Microsoft's docs seem to imply that Html.EditorFor is intended to be used only to render a single input, not a view: https://msdn.microsoft.com/en-us/library/system.web.mvc.html.editorextensions.editorfor(v=vs.118).aspx
Best Answer
Assume you have a model, like so:
Furthermore, assume you have this view, called ExampleModel.cshtml, in the EditorTemplates folder, in /Shared:
To continue our assumptions, let's say you have this model as well:
With a view for ExampleListModel, you can do something like this:
The Razor engine will look for (and find, in this case) a view that matches the name of the "item" class (in my example, ExampleModel). When it does, it will iterate through the collection, generating a row for each item in ExampleListModel.Examples. As a side effect, it also names the controls in such a way that the collection can be posted to the controller in a manner that the default model binder understands. So the generated markup may look like
and so on, with the indexes incrementing accordingly.