0

当我提交 Ajax.BeginForm 表单时,我在 Controller 上得到了 Model 的 null 值。模型内部有另一个模型(一对多关系)

这是我的示例视图代码(删除了不必要的东西)

 <table id="PageSortTable" class="travelads_business_rules" cellspacing="0">
<thead>
    <tr valign="bottom">
        <th>ID</th>
        <th>&nbsp;</th>
        <th>&nbsp;</th>
        <th>Rule Name</th>
        <th>Business</th>
        <th>Region</th>
        <th>Form</th>
        <th>Company</th>
        <th>Team</th>
        <th>Language</th>
        <th>Media</th>
        <th>InsertLoc</th>
        <th>Size</th>
        <th>Access</th>
    </tr>
</thead>
<tbody>
    @foreach (var item in Model)
    {
        <tr valign="top">
            <td class="col_a">
                <span>@item.id</span>
            </td>
            <td class="col_b">
                <a href="@(Url.Action("EditTravelAdsRule", new { id = item.id }))">
                    <img alt="Edit" class="tableItemImage" src="../../Content/Images/Edit-icon.PNG" title="Edit" />
                </a><a href="@(Url.Action("DeleteTravelAdsRule", new { id = item.id }))">
                    <img alt="Delete" class="tableItemImage" src="../../Content/Images/delete-icon.PNG"
                        title="Delete" />
                </a>
            </td>
            <td class="col_c">
                <div class="tableButtonCell"><a href="#" onclick='DoExpandCollapsh("trMinimised@(item.id)")'>
                        <img alt="Details" class="tableItemImage" src="../../Content/Images/expand-icon.png"
                            title="Expand" /></a></div>
            </td>
            <td class="col_d"><span>@item.ad_rule_name</span></td>
            <td class="col_f"><span>@item.business</span></td>
            <td class="col_g"><span>@item.ad_region</span></td>
            <td class="col_h"><span>@item.ad_form</span></td>
            <td class="col_i"><span>@item.ad_company</span></td>
            <td class="col_j"><span>@item.ad_team</span></td>
            <td class="col_k"><span>@item.ad_language</span></td>
            <td class="col_l"><span>@item.ad_media_id</span></td>
            <td class="col_m"><span>@item.ad_insert_loc</span></td>
            <td class="col_n"><span>&nbsp;</span></td>
            <td class="col_o"><span>@item.access</span></td>
        </tr>
        <tr class="minimised" id='trMinimised@(item.id)'>
            <td colspan="13">
                @using (Ajax.BeginForm("_SaveGroupMatchs", null, new AjaxOptions { OnSuccess = "onSuccess", UpdateTargetId = "msgResult", LoadingElementId = "msgLoading" }, new { @id = "SaveForm"}))
                {  

                <div class="detail">
                    <div class="table_header">
                        <span class="col_a">Group</span> <span class="col_b">Group Scope</span> <span class="col_c">
                            Seq</span> <span class="col_d">&nbsp;</span> <span class="col_e">ID</span> <span
                                class="col_f">Match Location</span> <span class="col_g">Target Field</span>
                        <span class="col_h">Match Type</span> <span class="col_i">Set Name</span>
                    </div>
                    <!-- Loop for matches -->
                    <!-- start GROUP -->
                    <div class="group">
                        @foreach (var item1 in item.lstMatches)
                        {
                            <div id="rule@(item.id)_divRuleGroup_@(item1.match_group_num)">
                            <header id="header_rule_Group"><span class="col_a">@(item1.match_group_num)  </span><span class="col_b">
                                            @Html.DropDownListFor(model => item1.match_group_scope, new SelectList((List<QIOnlineMVC3.Helpers.clsGroupScope>)QIOnlineMVC3.Helpers.General.GetALLGroupScope(), "GroupNum", "GroupScope", item1.match_group_num))
                                          </span></header>
                            <div class="match">
                                <span class="col_c">@(item1.match_group_seq) @Html.HiddenFor(model=>item1.match_group_seq) </span> <span class="col_d"><a href="#">
                                    <img src="../../Content/Images/delete-icon.png" width="17" height="17" border="0"
                                        title="delete" /></a> </span><span class="col_e">@(item1.match_id)
                                        </span><span class="col_f">
                                            @Html.DropDownListFor(model => item1.match_loc, new SelectList((List<QIOnlineMVC3.Helpers.clsMatchLocations>)QIOnlineMVC3.Helpers.General.GetALLMatchLocations(), "MatchLocations", "MatchLocations", item1.match_loc))
                                        </span><span class="col_g">
                                            @Html.DropDownListFor(model => item1.match_target_field, new SelectList((List<QIOnlineMVC3.Helpers.clsTargetField>)QIOnlineMVC3.Helpers.General.GetALTargetField(), "TargetField", "TargetField", item1.match_target_field))
                                        </span><span class="col_h">
                                            @Html.DropDownListFor(model => item1.match_type, new SelectList((List<QIOnlineMVC3.Helpers.clsMatchType>)QIOnlineMVC3.Helpers.General.GetALLMatchType(), "MatchTypeID", "MatchType", item1.match_type))
                                        </span><span class="col_i">
                                            @Html.TextBoxFor(model => item1.match_set_name, new { @value = item1.match_set_name, @class="clsauto" })
                                           @* <input type="text" name="sets" id="sets" class="clsauto" style="width:200px;" value='@(item1.match_set_name)'/>*@
                                            </span>
                            </div>
                            </div>
                        }
                        <!-- end MATCH -->
                        <div class="new">
                            <a href="#">
                                <img src="../../Content/Images/new-icon.png" width="17" height="17" border="0" alt="New Match" />New
                                Match</a></div>
                    </div>
                    <!-- end MATCH GROUP -->
                    <!-- END Loop of Matches -->
                    <div class="new new_group">
                        <img alt="" src="../../Content/Images/new-icon.png" width="17" height="17" border="0" />@Ajax.ActionLink("New Group", "GetGroupMatch", new AjaxOptions{ UpdateTargetId = "divNewGroup", HttpMethod = "Get", InsertionMode = InsertionMode.InsertAfter})
                        <div class="delete_group">
                            Delete Group ID:
                            <input type="text" id='txtdeleteGroupNo_@(item.id)' />
                            <input type="submit" id='btnDeleteGroup_@(item.id)' value="Delete Group" class="btn btn_delete" style="float: none;" onclick="return DeleteGroup(this);" />
                        </div>
                    </div>
                    <div id="divNewGroup">
                    </div>
                    <div class="btn_row">
                        <input type="reset" name="reset" class="btn" value="Cancel" onclick='DoExpandCollapsh("trMinimised@(item.id)","main_collapsed@(item.id)","main_expanded@(item.id)")'/>
                        <input type="submit" name="btnSave" class="btn" value="Save" />
                        <a href="@(Url.Action("DeleteTravelAdsRule", new { id = item.id }))" class="btn btn_delete">
                            Delete Rule</a>
                    </div>
                </div>
                }

            </td>
        </tr>

    }
</tbody>

这是我的控制器操作

  [Authorize]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult _SaveGroupMatchs(FormCollection fc, TravelAdsRulesMatchsGroups trmg)
    { 
   // trmg is null here... I  am getting form collection but its comma separated, very annoying to deal with that.
   }

任何人都可以有想法,我如何获得模型值。

这是我的班级信息

主类 TravelAds - 需要显示邮件记录(网格)

TravelAdsRulesMatchsGroups - 表内的第一个 foreach 循环类

MatchCollection - TravelAdsRulesMatchsGroups 中的第二个 foreach 循环类

我试图在控制器中获取带有值和子类(匹配集合)的 TravelAdsRulesMatchsGroups 对象,我认为无论如何都应该有。

提前致谢 - Amit Prajapati

4

1 回答 1

0

你写的那些foreach循环正在扼杀一切。请阅读您需要用于命名默认模型绑定器期望的输入元素的标准约定:http: //haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

现在查看生成的页面的 HTML 源代码,更具体地说是输入字段的名称。什么乱七八糟的,不是吗?没有什么符合默认模型绑定器的预期。当您使用表达式(例如model => item1.match_type生成的输入字段的名称)时,不会考虑复杂属性的当前导航上下文。

一种可能性是使用编辑器模板而不是那些 foreach 循环。这是我推荐的解决方案。我写了无数关于他们的帖子,只是谷歌

另一种可能性是修改模型以使用索引集合,然后使用for循环而不是foreach

@for (var i = 0; i < Model.Count; i++)
{
    ... some stuff ....

    @Html.TextBoxFor(x => x[i].SomeProperty)

    @for (var j = 0; j < Model[i].SomeCollectionProperty.Count; j++)
    {
        ... some stuff, ajax forms etc ....
        @Html.TextBoxFor(x => x[i].SomeCollectionProperty[j].SomeOtherProperty)
    }
}

注意现在使用的 lambda 表达式不仅包括导航属性名称,还包括索引。这将允许助手为输入字段生成正确的名称。

于 2012-07-27T08:08:58.623 回答