0

我正在尝试删除在视图中标记(检查)的条目,但不确定如何将集合传回控制器
我的模式是:
Group哪个有ICollection<SubGroup> SubGroups和子组有ICollection<Event> Events
我传递Group给视图并迭代和显示事件详细信息,包括一个复选框,因此如果选中它,则应删除事件条目。
当我将回发到控制器时,Group.SubGroups为空

  • 如何确保将子实体传递回控制器?
  • 我可以用@Html.CheckBoxOf 代替<input type="checkbox"...吗?

更新:模型

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; }
    ....
}  

我将Group作为模型传递给视图(见下文),并希望删除用户检查的事件

看法:

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

谢谢你

4

2 回答 2

0

尝试这个

 public ActionResult ViewName(Group model)
    {
       if(model != null && ModelState.IsValid)
       {
           //delete....
       }
       return....
    }
于 2013-08-14T11:12:25.597 回答
0

尝试这个...

 public ActionResult ViewName(FormCollection collection)
    {
       if(collection['eventToDelete']!=null && collection['eventToDelete'].ToString()!="")
       {
           //delete....
       }
       return....
    }
于 2013-08-14T10:48:53.973 回答