【问题标题】:EditorForModel() not workingEditorForModel() 不工作
【发布时间】:2015-06-18 02:51:52
【问题描述】:

我正在使用 EditorForModel() 来填充数据

索引方法:

public ActionResult Index()
{
    SampleDbContext db = new SampleDbContext();
    return View(db.Employees.ToList());
}

索引视图:

@model IEnumerable<MultipleRowsDemo.Models.Employee>

<div style="font-family:Aria">

@using (Html.BeginForm())
{ 
    <table border="1">
        <thead>
            <tr>
                <th>
                    Select
                </th>
                <th>
                    Name
                </th>
                <th>
                    Email
                </th>
            </tr>
        </thead>
        <tbody>
        @Html.EditorForModel()
        </tbody>
    </table>
}

</div>

但它不起作用。它显示类似的数据

123 选择姓名电子邮件

我看过类似的帖子

EditorForModel and DisplayForModel do not work then passing class model within class model

建议不要使用复杂的属性。我没有。

另一个是。

Displaying entities data with EditorForModel

建议使用

@using(Html.BeginForm())
{
...
}

我已经有了。

【问题讨论】:

  • 我现在无法对此进行测试,但我认为 EditoForModel 不应该适用于复杂对象的 IEnumerable(即您创建的某个类的实例) )。保留此代码并在您的Views/Shared/EditorTemplates 文件夹中创建一个Employee.cshtml(创建EditorTemplatesFolder,默认情况下它不存在)但如果您使用的是Areas,请将文件放在YourAreaName/Views/Shared/EditorTemplates 中。还有一个 Views/Shared/DisplayTemplates Equivalent 用于显示模板。

标签: asp.net-mvc entity-framework razor


【解决方案1】:

您的类实体名称应与模板名称匹配 例如:如果您的 ADO.net Employee 实体名称是“Employee”,那么模板的名称应该是 Views 目录下 Shared 文件夹中的“Employee.cshtml”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    相关资源
    最近更新 更多