当我提交Ajax.BeginForm表单时,我在Controller上获取Model的空值。模型里面有另一个模型(一对多关系)
这是我的Sample View代码(删除了不必要的东西)
<table id="PageSortTable" class="travelads_business_rules" cellspacing="0">
<thead>
<tr valign="bottom">
<th>ID</th>
<th> </th>
<th> </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> </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"> </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.
}
任何人都可以拥有Idea,我如何获得Model值。
这是我的班级信息
主要类TravelAds - 显示邮件记录(网格)所需
TravelAdsRulesMatchsGroups - 表格中的第一个foreach循环类
MatchCollection - TravelAdsRulesMatchsGroups内的第二个foreach循环类
我试图在控制器中使用值和子类(匹配集合)获取TravelAdsRulesMatchsGroups对象,我认为应该有。
先谢谢 - Amit Prajapati
答案 0 :(得分:0)
你写的那些foreach
循环正在杀死一切。请阅读有关命名默认模型绑定器所需的输入元素所需的标准约定:http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
现在查看生成的页面HTML源代码,更具体地说是输入字段的名称。真是一团糟,不是吗?没有什么能与默认模型绑定器所期望的相匹配使用model => item1.match_type
等表达式时,生成的输入字段的名称不会考虑复杂属性的当前导航上下文。
一种可能性是使用编辑器模板而不是那些foreach循环。这是我推荐的解决方案。我写了很多关于他们的文章,只是Google。
另一种可能性是修改模型以使用索引集合,然后使用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表达式不仅包括导航属性名称,还包括索引。这将允许帮助程序为输入字段生成适当的名称。