views:

229

answers:

1

Using EditorFor( model lamda, "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></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>
A: 

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.

Dr. Zim
did you ever have any luck using template html helpers with collections?
kdawg
Yes. This link was very helpful http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/. It mentions template helpers. He randomizes the index, but I don't think MVC 2 requires it, which means you could use EditorFor and DisplayFor instead of his custom HTML helper.
Dr. Zim