0

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
my mode is:
Group which has ICollection<SubGroup> SubGroups and SubGroup has ICollection<Event> Events
I pass Group to the view and iterate and display Event details including a checkbox so if it's checked the event entry should be deleted.
When I get the postback to the controller, Group.SubGroups is null

  • How do I make sure the child entities are passed back to the controller?
  • Can I use @Html.CheckBox instead Of <input type="checkbox"... ?

Update: Model

public class Group
{
    [Key]
    public int GroupId { get; set; }
    public virtual IList<SubGroup> SubGroups { get; set; }
    ....
}

public class SubGroup
{
    [Key]
    public int SubGroupId { get; set; }
    public virtual IList<Event> Events { get; set; }
    ....
}

public class Events
{
    [Key]
    public int EventId { get; set; }
    public string EventName { get; set; }
    public bool IsDeleted { get; set; }
    ....
}  

I am passing Group to the view (see below) as the Model and want to delete events which are checked by the user

View:

@using System.Globalization
@model NS.Models.Group
@{
    ViewBag.Title = "Edit";
}
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Booking Details</legend>
        <div class="display-label">
            Group Name
        </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.GroupName)
    </div>
    <div class="display-field">

        @foreach (var b in Model.SubGroup)
        {
            groupNo += 1;
            <table class="main" style="width: 80%; margin-top: 10px">
                <tr>
                    <th>
                        @Html.DisplayName("Sub Group ")
                        @Html.DisplayName(b.SubGroupName)
                    </th>

                </tr>
                <table class="main" style="width: 80%;">
                    <tr>
                        <th>Event</th>
                        <th>Delete</th>
                    </tr>
                    @foreach (var ev in b.Events)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => ev.EventName)
                            </td>
                            <td>
                                <input type="checkbox" id="eventToDelete" name="eventToDelete" value="@ev.EventId" />
                            </td>
                        </tr>
                    }
                </table>
            </table>
        }
    </div>
    <p>
        <input type="submit" name="xc" value="Delete" class="button" />
    </p>
</fieldset>
}  

Thank You

melspring
  • 123
  • 14
  • possible duplicate of [CheckboxList in MVC3 View and get the checked items passed to the controller](http://stackoverflow.com/questions/5284395/checkboxlist-in-mvc3-view-and-get-the-checked-items-passed-to-the-controller) – Imad Alazani Aug 14 '13 at 11:06
  • The above does not deal with navigation property. In my case I want to delete item from navigation property, which is 2 levels down from the parent – melspring Aug 14 '13 at 13:26
  • please see the update, I have added the model – melspring Aug 15 '13 at 00:00

2 Answers2

0

Try this...

 public ActionResult ViewName(FormCollection collection)
    {
       if(collection['eventToDelete']!=null && collection['eventToDelete'].ToString()!="")
       {
           //delete....
       }
       return....
    }
0

Try this

 public ActionResult ViewName(Group model)
    {
       if(model != null && ModelState.IsValid)
       {
           //delete....
       }
       return....
    }
Jaimin
  • 7,964
  • 2
  • 25
  • 32
  • Entry to be deleted is 2 levels down from the model, ie:Group.SubGroup.Event and subgroup is null so cannot do this – melspring Aug 14 '13 at 13:22
  • @melspring can you post your model.So we can understand what is your model structure. – Jaimin Aug 14 '13 at 13:31