【问题标题】:Passing back child entity from MVC view to controller将子实体从 MVC 视图传递回控制器
【发布时间】:2013-08-15 07:56:17
【问题描述】:

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

  • 如何确保将子实体传递回控制器?
  • 我可以使用@Html.CheckBox 代替<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>
}  

谢谢

【问题讨论】:

标签: asp.net-mvc razor


【解决方案1】:

试试这个...

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

【讨论】:

    【解决方案2】:

    试试这个

     public ActionResult ViewName(Group model)
        {
           if(model != null && ModelState.IsValid)
           {
               //delete....
           }
           return....
        }
    

    【讨论】:

    • 要删除的条目比模型低2级,即:Group.SubGroup.Event和子组为空,所以不能这样做
    • @melspring 你能发布你的模型吗?这样我们就可以了解你的模型结构是什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    相关资源
    最近更新 更多