开发者

ASP.NET MVC 2 UI Templates displaying data without markup. How to fix?

Using EditorFor( model lambda, "viewTemplateName"), my output is completely not as expected. This doesn't produce any errors, but it is rendering output without markup. What am I doing wrong?

The Output:

HarryTomRichard

The Expected Output (I need to figure out how to render the List [] indexes on id too but not to that problem yet):

<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&开发者_如何学Pythongt;</td></tr>
</table>

My Classes:

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

My Controller:

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);
}

My Index View:

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

My Classroom Template (notice the m => item so I can use the item, not the model):

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>

My Student Template:

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


jfar has the answer, and I will mark it appropriately when added. The solution was simply to ensure the files were located in Views->ControllerName->EditorTemplates and Views->ControllerName->DisplayTemplates. These can also be located in the Shared folder too.

I like this post. Now I need to learn how to use the MVC 2 template Html helpers that reference collections. It's in MVC 2 RC.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜