Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing back child entity from MVC view to controller
    text
    copied!<p>I'm trying to delete entries which are marked (checked) in view, but not sure how to pass back the collection back to the controller<br> my mode is:<br> <code>Group</code> which has <code>ICollection&lt;SubGroup&gt; SubGroups</code> and SubGroup has <code>ICollection&lt;Event&gt; Events</code><br> I pass <code>Group</code> to the view and iterate and display Event details including a checkbox so if it's checked the event entry should be deleted.<br> When I get the postback to the controller, <code>Group.SubGroups</code> is null </p> <ul> <li>How do I make sure the child entities are passed back to the controller?</li> <li>Can I use <code>@Html.CheckBox</code> instead Of <code>&lt;input type="checkbox"...</code> ? </li> </ul> <p><strong>Update:</strong> Model </p> <pre><code>public class Group { [Key] public int GroupId { get; set; } public virtual IList&lt;SubGroup&gt; SubGroups { get; set; } .... } public class SubGroup { [Key] public int SubGroupId { get; set; } public virtual IList&lt;Event&gt; Events { get; set; } .... } public class Events { [Key] public int EventId { get; set; } public string EventName { get; set; } public bool IsDeleted { get; set; } .... } </code></pre> <p><strong>I am passing <em>Group</em> to the view (see below) as the Model and want to delete events which are checked by the user</strong> </p> <p>View: </p> <pre><code>@using System.Globalization @model NS.Models.Group @{ ViewBag.Title = "Edit"; } @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;Booking Details&lt;/legend&gt; &lt;div class="display-label"&gt; Group Name &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(model =&gt; model.GroupName) &lt;/div&gt; &lt;div class="display-field"&gt; @foreach (var b in Model.SubGroup) { groupNo += 1; &lt;table class="main" style="width: 80%; margin-top: 10px"&gt; &lt;tr&gt; &lt;th&gt; @Html.DisplayName("Sub Group ") @Html.DisplayName(b.SubGroupName) &lt;/th&gt; &lt;/tr&gt; &lt;table class="main" style="width: 80%;"&gt; &lt;tr&gt; &lt;th&gt;Event&lt;/th&gt; &lt;th&gt;Delete&lt;/th&gt; &lt;/tr&gt; @foreach (var ev in b.Events) { &lt;tr&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; ev.EventName) &lt;/td&gt; &lt;td&gt; &lt;input type="checkbox" id="eventToDelete" name="eventToDelete" value="@ev.EventId" /&gt; &lt;/td&gt; &lt;/tr&gt; } &lt;/table&gt; &lt;/table&gt; } &lt;/div&gt; &lt;p&gt; &lt;input type="submit" name="xc" value="Delete" class="button" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } </code></pre> <p>Thank You</p>
 

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