1

我在表格的每一行都有@Ajax.BeginForm,当我使用该表格发出发布请求时,除第一行外,模型为空。

只有第一个表格有效,第二个表格无效......我使用了唯一的表格ID。

可能是因为我使用的是 IList 和 for 循环,

AND 第一个表单具有控件名称,如 [0].lstMatches[4].match_set_name 和第二、第三、....表单具有控件名称,如 [n].lstMatches[4].match_set_name

并且 IList 只能为第一个提供模型,[0].lstMatches[4].match_set_name

这是我的视图结构

  @model IList<QIOnlineMVC3.Models.TravelAdsRulesMatchsGroups>
@{
ViewBag.Title = "Business Rule Travel Ads";
ViewBag.Layout = "_Layout2";
 }

 <table id="PageSortTable" class="travelads_business_rules" cellspacing="0">
<tr></tr>
 <tbody>
@for (var i = 0; i < Model.Count; i++)
    {
<tr class="minimised" id='trMinimised@(Model[i].id)'>
            <td colspan="13">
                @using (Ajax.BeginForm("_SaveGroupMatchs", null, new AjaxOptions { OnSuccess = "onSuccess", UpdateTargetId = "msgResult", LoadingElementId = "msgLoading", HttpMethod = "Post" }, new { @id = "SaveForm" + Model[i].id.ToString() }))
                {  
 <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" id="Group_rule_@(Model[i].id)">

                       @Html.HiddenFor(model => @Model[i].id)
                       @Html.HiddenFor(model => @Model[i].ad_rule_name)
                       @Html.HiddenFor(model => @Model[i].ad_rule_desc)
                       @Html.HiddenFor(model => @Model[i].business)
                       @Html.HiddenFor(model => @Model[i].ad_region)
                       @Html.HiddenFor(model => @Model[i].ad_form)
                       @Html.HiddenFor(model => @Model[i].ad_company)
                       @Html.HiddenFor(model => @Model[i].ad_team)
                       @Html.HiddenFor(model => @Model[i].ad_language)
                       @Html.HiddenFor(model => @Model[i].ad_media_id)
                       @Html.HiddenFor(model => @Model[i].ad_insert_loc)
                       @Html.HiddenFor(model => @Model[i].ad_priority)
                       @Html.HiddenFor(model => @Model[i].access)
                       @Html.HiddenFor(model => @Model[i].deployment)


                        @for (var j = 0; j < Model[i].lstMatches.Count; j++)
                        {
                            <div id="rule@(Model[i].id)_divRuleGroup_@(Model[i].lstMatches[j].match_group_num)">
                             @Html.HiddenFor(model => Model[i].lstMatches[j].match_rule_id)
                             @Html.HiddenFor(model => Model[i].lstMatches[j].match_id)
                             @Html.HiddenFor(model => Model[i].lstMatches[j].match_group_num)
                             @Html.HiddenFor(model => Model[i].lstMatches[j].match_rule_type)
                             @Html.HiddenFor(model => Model[i].lstMatches[j].created)
                             @Html.HiddenFor(model => Model[i].lstMatches[j].access)
                            <header id="header_rule_Group"><span class="col_a">
                             @if (Model[i].lstMatches[j].Position == "First")
                             {
                                @(Model[i].lstMatches[j].match_group_num) 
                             } </span>
                            <span class="col_b">
                                            @if (Model[i].lstMatches[j].Position == "First")
                                            {
                                            @Html.DropDownListFor(model => Model[i].lstMatches[j].match_group_scope, new SelectList((List<QIOnlineMVC3.Helpers.clsGroupScope>)QIOnlineMVC3.Helpers.General.GetALLGroupScope(), "GroupScope", "GroupScope", Model[i].lstMatches[j].match_group_scope))
                                            }
                                            else
                                            {
                                                @Html.HiddenFor(model => Model[i].lstMatches[j].match_group_scope)
                                            }
                                          </span></header>
                            <div class="match">
                                <span class="col_c">@(Model[i].lstMatches[j].match_group_seq) @Html.HiddenFor(model => Model[i].lstMatches[j].match_group_seq) </span> 
                                <span class="col_d">
                                <a href="@(Url.Action("DeleteMatch", new { id = Model[i].lstMatches[j].match_id }))">
                                    <img alt="Delete" src="../../Content/Images/delete-icon.png" width="17" height="17" border="0" title="delete" /></a> 

                                        </span><span class="col_e">@(Model[i].lstMatches[j].match_id)
                                        </span><span class="col_f">
                                            @Html.DropDownListFor(model => Model[i].lstMatches[j].match_loc, new SelectList((List<QIOnlineMVC3.Helpers.clsMatchLocations>)QIOnlineMVC3.Helpers.General.GetALLMatchLocations(), "MatchLocations", "MatchLocations", Model[i].lstMatches[j].match_loc))
                                        </span><span class="col_g">
                                            @Html.DropDownListFor(model => Model[i].lstMatches[j].match_target_field, new SelectList((List<QIOnlineMVC3.Helpers.clsTargetField>)QIOnlineMVC3.Helpers.General.GetALTargetField(), "TargetField", "TargetField", Model[i].lstMatches[j].match_target_field))
                                        </span><span class="col_h">
                                            @Html.DropDownListFor(model => Model[i].lstMatches[j].match_type, new SelectList((List<QIOnlineMVC3.Helpers.clsMatchType>)QIOnlineMVC3.Helpers.General.GetALLMatchType(), "MatchTypeID", "MatchType", Model[i].lstMatches[j].match_type))
                                        </span><span class="col_i">
                                            @Html.TextBoxFor(model => Model[i].lstMatches[j].match_set_name, new { @value = Model[i].lstMatches[j].match_set_name, @class = "clsauto" })
                                            </span></div>
                            </div>


                                       if (Model[i].lstMatches[j].Last == "Last")
                                       {
                            <div id="Group_rule_@(Model[i].id)_@(Model[i].lstMatches[j].match_group_num)"></div>
                            <!-- end MATCH -->
                            <div class="new">
                                @Ajax.ImageWithTextActionLink("<img src=\"../../Content/Images/new-icon.png\" width=\"17\" height=\"17\" border=\"0\" alt=\"New Match\" />New Match", "NewMatch", new { ruleid = Model[i].id, groupid = Model[i].lstMatches[j].match_group_num }, new AjaxOptions { UpdateTargetId = "Group_rule_" + Model[i].id.ToString() + "_" + Model[i].lstMatches[j].match_group_num, InsertionMode = InsertionMode.InsertBefore, HttpMethod = "Get", LoadingElementId = "msgLoading" })
                            </div>
                                       }

                        }

                    </div>
                    <!-- end MATCH GROUP -->
                    <!-- END Loop of Matches -->
                    <div class="new new_group">
                        @Ajax.ImageWithTextActionLink("<img src=\"../../Content/Images/new-icon.png\" width=\"17\" height=\"17\" border=\"0\" alt=\"New Group\" />New Group</a>", "NewGroup", new { ruleid = Model[i].id }, new AjaxOptions { UpdateTargetId = "Group_rule_" + Model[i].id.ToString(), InsertionMode = InsertionMode.InsertAfter, HttpMethod = "Get", LoadingElementId = "msgLoading" })
                        <div class="delete_group">
                            Delete Group ID:
                            <input type="text" id='txtdeleteGroupNo_@(Model[i].id)' />
                            <input type="submit" id='btnDeleteGroup_@(Model[i].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@( Model[i].id)","main_collapsed@( Model[i].id)","main_expanded@( Model[i].id)")'/>
                        <input type="submit" name="btnSave" class="btn" value="Save" />
                        <a href="@(Url.Action("DeleteTravelAdsRule", new { id = Model[i].id }))" class="btn btn_delete">
                            Delete Rule</a>
                    </div>
                </div>
                }

            </td>
        </tr>

    }
</tbody>

这是我的控制器

    [Authorize]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult _SaveGroupMatchs(IList<TravelAdsRulesMatchsGroups> mm)
    {
        try
        {
            if (mm != null & mm.Count > 0)
            {

                foreach (var item in mm)
                {
                    List<MatchModel> LMM = item.lstMatches.ToList();
                    List<MatchModel> LMMUpdate = LMM.Where(t => t.match_id != 0).ToList();
                    //update 
                    foreach (var mmupdate in LMMUpdate)
                    {
                        UpdateMatch(mmupdate);
                    }
                    List<MatchModel> LMMSave = LMM.Where(t => t.match_id == 0).ToList();
                    //save
                    foreach (var itemSave in LMMSave)
                    {
                        itemSave.access = item.access;
                        itemSave.deployment = item.deployment;
                        itemSave.match_deployment = item.deployment;
                        CreateMatch(itemSave);
                    }
                }
            }
        }
        catch { Content("Problem due to Error"); }

        return Content("Record Saved Successfully!");
    }

我没有得到第二个 IList 等行......

我怎样才能得到它,有什么想法吗?

4

1 回答 1

0

嗯,我真的不明白你想做什么。

  • 如果您想保存TravelAdsRulesMatchsGroups
    仅使用一个调用您的_SaveGroupMatchs操作方法 的表单的 wole 列表

  • 如果您只想保存一个TravelAdsRulesMatchsGroups(每条记录一个提交按钮),
    请继续每行使用一个form并将控制器的操作更改为

    public ActionResult _SaveGroupMatchs(TravelAdsRulesMatchsGroups mm)
    

    然后调整你的动作方法来处理一个实例的记录TravelAdsRulesMatchsGroups

    而且,在您的视图中,不要使用

    @Html.HiddenFor(model => model[i].Id)
    

    如果您使用此语法,则呈现的 Html 将是<input ... name="[i].Id" />
    (例如。<input ... name="[2].Id" />

    模型绑定器使用name来填充模型的属性

    相反,使用

    @* for your TravelAdsRulesMatchsGroups properties *@
    @Html.Hidden("id", Model[i].id)
    @* ... and so on ...*@
    
    
    @* for the lstMatches property, which is a list of MatchModel *@
    @for (var j = 0; j < Model[i].lstMatches.Count; j++)
    {
        @Html.Hidden("lstMatches[" + j.ToString() + "].match_rule_id", Model[i].lstMatches[j].match_rule_id)
        @* ... and so on ...*@
    }
    

注意:小心,您不能使用嵌套的 Html 表单!

于 2012-08-03T10:06:43.283 回答