使用EditorFor(型号拉姆达,“viewTemplateName”),我的输出是完全不按预期方式。这不会产生任何错误,但它的渲染输出,无需标记。我究竟做错了什么?

输出:

HarryTomRichard

预期的输出(我需要弄清楚如何呈现的ID名单[]指标太多,但没有对这个问题还没有):

<table>
    <tr><td><span><input type="Text" id="Name[0]" value="Harry" /></span></td></tr>
    <tr><td><span><input type="Text" id="Name[1]" value="Tom" /></span></td></tr>
    <tr><td><span><input type="Text" id="Name[2]" value="Richard" /></span></td></tr>
</table>

我的职业:

namespace Marcs.Models {
    public class Student   { public string Name { get; set; } }
    public class Classroom { public List<Student> Students { get; set; }
}

我的控制器:

public ActionResult Index() {
    var myStudents = new List<Student>();
    myStudents.Add(new Student { Name = "Harry" });
    myStudents.Add(new Student { Name = "Tom" });
    myStudents.Add(new Student { Name = "Richard" });
    var myClass = new Classroom {Students = myStudents};
    return View(myClass);
}

我的索引视图:

Inherits="System.Web.Mvc.ViewPage<Marcs.Models.Classroom>" %>
<% using (Html.BeginForm()) { %>
    <%= Html.EditorFor(m => m.Students, "Classroom") %>
    <input type="submit" value="Save" />
<% } %>

我的课堂模板(通知第m =>项,所以我可以使用该项目,而不是模型):

Inherits="System.Web.Mvc.ViewUserControl<List<Marcs.Models.Student>>" %>
<table>
    <% foreach (Marcs.Models.Student item in Model)
    { %><tr><td><%= Html.EditorFor(m => item, "Student")%></td></tr><%
    } %>
</table>

我的学生模板:

Inherits="System.Web.Mvc.ViewUserControl<Marcs.Models.Student>"
%><span><%= Html.Encode( Html.EditorFor( m => m.Name)) %></span>
有帮助吗?

解决方案

jfar有答案,并添加时,我会适当地将其标记。该解决方案的只是确保这些文件都位于视图 - > ControllerName-> EditorTemplates和视图 - > ControllerName-> DisplayTemplates。这些也可以位于在共享文件夹太

我喜欢这个讯息。现在,我需要学习如何使用MVC 2模板HTML辅助该参考集合。这是一个在MVC 2 RC。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top