【问题标题】:MVC EditorFor only rendering single property, not EditorTemplateMVC EditorFor只渲染单个属性,而不是EditorTemplate
【发布时间】:2016-03-22 20:54:56
【问题描述】:

EditorFor 出现一些问题,我无法理解这种行为。

我有一个对象列表,我想根据该列表呈现编辑器模板。

根据https://stackoverflow.com/a/26069912,EditorFor 应该为每个对象渲染模板。

  public class Person
{
    public string Name { get; set; }
    public string PhoneNumber { get; set; }
}

模板

@model EditForTest.Models.Person

Name
<td>@Html.TextBoxFor(x => x.Name)</td>
Phone
<td>@Html.TextBoxFor(x => x.PhoneNumber)</td>

在视图中,我得到了一个包含人员列表的视图模型。当我使用

@Html.EditorFor(x => x.Persons)

模板未呈现,并且以某种方式在页面上呈现了第一个属性。

【问题讨论】:

  • 您是否正确命名了模板? - /Views/Shared/EditorTemplates/Person.cshtml
  • Persons 会不会是 Person 对象的 List?我很确定编辑器模板不会那样工作。您必须手动遍历列表并调用 EditorFor。
  • 我更喜欢PartialView
  • @StephenMuecke 那是我的问题,我的模板不在正确的文件夹中。一旦我移动它,字段就会为列表中的每个对象正确呈现。

标签: asp.net-mvc editorfor editorfortemplate


【解决方案1】:

正如@StephenMuecke 所说,我的编辑器模板有问题。 将模板移动到正确的路径 /Views/Shared/EditorTemplates/Person.cshtml 后,编辑器就会正确呈现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    相关资源
    最近更新 更多