简体   繁体   English

为什么使用EditorFor over PartialView在MVC 4.5+中渲染局部视图

[英]Why use EditorFor over PartialView to render a partial view in MVC 4.5+

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. 我正在使用ASP.NET MVC 4.5+,我试图理解为什么人们想要使用Html.EditorFor而不是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. 我发现的是EditorFor“尊重模型层次结构”,我收集的意思是,对于EditorFor呈现的视图中的输入,输入名称/ ID反映了调用模型的嵌套级别,以及PartialViews不要这样做。

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+. 通过Html.PartialView呈现时,至少在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. 据我所知,DisplayFor和EditorFor将告诉框架自动查看〜/ * Templates文件夹,以返回这些控件调用的视图的视图。 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. 所以,我现在唯一能想到的就是我们使用Display / EditorFor来允许这种文件/文件夹结构和自动渲染,这比在“共享”文件夹中查找更具语义性。特别命名为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 就目前而言,即使微软的文档似乎暗示Html.EditorFor仅用于呈现单个输入,而不是视图: https ://msdn.microsoft.com/en-us/library/system.web .mvc.html.editorextensions.editorfor(v = vs.118)的.aspx

Assume you have a model, like so: 假设您有一个模型,如下所示:

public class ExampleModel
{
    public int ID { get; set; }
    public string Name { get; set; }
}

Furthermore, assume you have this view, called ExampleModel.cshtml, in the EditorTemplates folder, in /Shared: 此外,假设您在EditorTemplates文件夹中的/ Shared中有这个名为ExampleModel.cshtml的视图:

@model ExampleModel

@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
@Html.HiddenFor(m => m.ID)

To continue our assumptions, let's say you have this model as well: 为了继续我们的假设,让我们说你也有这个模型:

public class ExampleListModel
{
    public ICollection<ExampleModel> Examples { get; set; }
}

With a view for ExampleListModel, you can do something like this: 通过ExampleListModel的视图,您可以执行以下操作:

@model ExampleListModel

@Html.EditorFor(m => m.Examples)

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). Razor引擎将查找(并在本例中查找)与“item”类的名称匹配的视图(在我的示例中为ExampleModel)。 When it does, it will iterate through the collection, generating a row for each item in ExampleListModel.Examples. 如果是这样,它将遍历集合,为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 因此生成的标记可能看起来像

<label for="Examples[0].Name">Name</label>
<input id="Examples[0].Name" name="Examples_0__Name" value="Fee" />
<input id="Examples[0].ID" name="Examples_0__ID" value="1" />

<label for="Examples[1].Name">Name</label>
<input id="Examples[1].Name" name="Examples_1__Name" value="Fi" />
<input id="Examples[1].ID" name="Examples_1__ID" value="2" />

<label for="Examples[2].Name">Name</label>
<input id="Examples[2].Name" name="Examples_2__Name" value="Fo" />
<input id="Examples[2].ID" name="Examples_2__ID" value="3" />

and so on, with the indexes incrementing accordingly. 依此类推,索引递增。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM