Domanda

Utilizzando EditorFor (modello lambda, "viewTemplateName"), la mia uscita è del tutto non come previsto. Questo non produce eventuali errori, ma si sta rendendo uscita senza markup. Che cosa sto facendo di sbagliato?

L'output:

HarryTomRichard

Il risultato atteso (ho bisogno di capire come rendere i [] indici Elenca id troppo, ma non a questo problema ancora):

<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>

Le mie classi:

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

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

La mia Indice Vista:

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

La mia aula Template (notare il m => voce in modo da poter usare la voce, non il modello):

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>

La mia Student Template:

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

Soluzione

jfar ha la risposta, e mi segnerà in modo appropriato quando aggiunto. La soluzione era semplicemente quello di assicurare che i file erano situati in Views-> ControllerName-> EditorTemplates e Views->> ControllerName- DisplayTemplates. Questi possono anche essere situati nella cartella condivisa anche.

Mi piace questo post. Ora ho bisogno di imparare a utilizzare gli aiutanti Html MVC 2 modello che fanno riferimento a collezioni. E 'in MVC 2 RC.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top