Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple models in one form using EditorFor
    primarykey
    data
    text
    <p>I'm trying to create a simple multi model form using .net 4.5, Entity 5, MVC 4</p> <p>Based on examples I have seen I expect EditorTemplates\Tasks.cshtml to be rendered twice, once for each Task in the Project, but it only appears to get called once and passed a collection of Tasks which I seem to be unable to work with. </p> <p>Any ideas? My classes/views are listed below.</p> <p>Task:</p> <pre><code>public partial class Task { public Task() { this.TaskPeople = new HashSet&lt;TaskPerson&gt;(); } public int Id { get; set; } public string Title { get; set; } public int ProjectId { get; set; } public virtual Project Project { get; set; } public virtual ICollection&lt;TaskPerson&gt; TaskPeople { get; set; } } </code></pre> <p>Project:</p> <pre><code>public partial class Project { public Project() { this.Tasks = new HashSet&lt;Task&gt;(); } public int Id { get; set; } public string Title { get; set; } public virtual ICollection&lt;Task&gt; Tasks { get; set; } } </code></pre> <p>HomeController.New():</p> <pre><code>public ActionResult New() { Project project = new Project(); project.Tasks.Add(new Task() { }); project.Tasks.Add(new Task() { }); return View(project); } </code></pre> <p>Home/New.cshtml:</p> <pre><code>@using (Html.BeginForm("Create", "Home", FormMethod.Post)) { @Html.AntiForgeryToken() @Html.ValidationSummary() @Html.LabelFor(m =&gt; m.Title) @Html.EditorFor(m =&gt; m.Tasks, "Tasks") &lt;input type="submit" value="Save" /&gt; } </code></pre> <p>Home/EditorTemplates/Tasks.cshtml</p> <pre><code>@model IEnumerable&lt;MvcApplication5.Models.Task&gt; @Html.TextBoxFor(m =&gt; m.Title) // Error Here </code></pre> <p>EDIT:</p> <p>If I take IEnumerable out of Tasks.cshtml and replace it with</p> <pre><code>@model MvcApplication5.Models.Task </code></pre> <p>I get the following exception: </p> <pre><code>The model item passed into the dictionary is of type 'System.Collections.Generic.HashSet`1[MvcApplication5.Models.Task]', but this dictionary requires a model item of type 'MvcApplication5.Models.Task'. </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload