我有一个MVC页面正在做一些奇怪的事情。我有一个包含两个IList,分支和用户的类。
public class ThirdPartyViewModel
{
public String MessageText { get; set; }
public BranchAddress Address { get; set; }
public String AddressBranchCode { get; set; }
public String FullAddressBlock { get; set; }
public IList<ThirdPartySubUserLine> ThirdPartyUsers { get; set; }
public IList<ThirdPartyBranch> ThirdPartyBranches { get; set; }
public String BrandStyle { get; set; }
}
该视图包含2x标签和2x带模板的EditorFor
<div id="tabContainer" class="container">
<ul class="tabs">
<li class="tab-link current" data-tab="tab1">Users</li>
<li class="tab-link" data-tab="tab2">Branches</li>
</ul>
<div id="tab1" class="tab-content current">
<table>
<tbody id="mainList" style="display: block; overflow: auto;">
@Html.EditorFor(m => m.ThirdPartyUsers, new { editor = "users" })
</tbody>
</table>
</div>
<div id="tab2" class="tab-content">
<table>
<tbody id="mainListSec" style="display: block; overflow: auto;">
@Html.EditorFor(t => t.ThirdPartyBranches, new { editor = "data" })
</tbody>
</table>
</div>
<input id="btnSave" name="Save" type="submit" title="SAVE" />
</div>
2x模板看起来像这样:
@model MVC.Controllers.ThirdPartyController.ThirdPartyBranch
<tr style="vertical-align: top;" lineType="@(ViewData["editor"])">
<td style="width: 200px; text-align: left; font-size: x-small;">
@Html.HiddenFor(t => t.Changed, new { @class = ViewData["editor"] + "RowChanged", value="false" })
@(Model.BranchText)
@Html.HiddenFor(t => t.BranchText, new { @class="br_name" })
</td>
<td style="width: 120px; text-align: left; font-size: x-small;">
@Html.DisplayFor(t => t.BranchTown)
@Html.HiddenFor(t => t.BranchTown, new { @class="br_town" })
</td>
<td style="width: 80px; text-align: center; font-size: x-small;">
@Html.DisplayFor(t => t.BranchPostalCode)
@Html.HiddenFor(t => t.BranchPostalCode, new { @class="br_postcode" })
</td>
<td style="width: 80px; text-align: center; font-size: x-small;">
@Html.DisplayFor(t => t.Branch)
@Html.HiddenFor(t => t.Branch, new { @class="br_code" })
</td>
<td style="width: 90px; text-align: center;">
@Html.CheckBoxFor(t => t.isFlaggedAsRetailer, new { @class = "incLabour" })
</td>
</tr>
@model MVC.Controllers.ThirdPartyController.ThirdPartySubUserLine
<tr style="vertical-align: top;" lineType="@(ViewData["editor2"])">
<td style="width: 200px; text-align: left; font-size: x-small;">
@Html.HiddenFor(m => m.Changed, new { @class = ViewData["editor2"] + "RowChanged2", value="false" })
@Html.DisplayFor(m => m.UserID)
@Html.HiddenFor(m => m.UserID, new { @class="br_userid" })
</td>
<td style="width: 120px; text-align: left; font-size: x-small;">
@Html.DisplayFor(m => m.UserFirstName)
@Html.HiddenFor(m => m.UserFirstName, new { @class="br_userfname" })
</td>
<td style="width: 80px; text-align: center; font-size: x-small;">
@Html.DisplayFor(m => m.UserLastName)
@Html.HiddenFor(m => m.UserLastName, new { @class="br_userlname" })
</td>
<td style="width: 90px; text-align: center;">
@Html.CheckBoxFor(m => m.isFlaggedAsRetailer, new { @class = "allowRetailer" })
</td>
</tr>
我遇到的问题是在控制器中。我没有让viewmodel一直回来。
[HttpPost]
[Button(ButtonName = "Save")]
[ActionName("List")]
public ActionResult Save(ThirdPartyViewModel postedViewModel)
{
//somecode
}
在控制器中,在'Save'ActionResult下,我并不总是得到两个IList。这就是我所知道的:
我希望我已经提供了足够的信息给某人发现我犯的错误但如果没有请问。我希望这是愚蠢的事情。我一直在看这个太久了。有什么想法吗?
UPDATE:
昨天大部分时间都在看这个。我联系了Fiddler并观看了回发的内容。在所有情况下,两个IList的详细信息都会被回发,但当您将鼠标悬停在vs2013中的回发视图模型上时,分支IList在某些条件下为空。如果你查看'Request.Form',你每次都可以看到结果,所以我做了一个Dirty Hack(见下文)。通过查看Fiddler我还注意到'CheckBoxFor'控件返回'False'或'False,True',所以我已经处理过了。
仍然提出问题为什么?
if (postedViewModel.ThirdPartyBranches != null)
{
foreach (ThirdPartyBranch item in postedViewModel.ThirdPartyBranches)
{
//Do Update;
}
}
else
{
if (Request.Form.AllKeys.Contains("ThirdPartyBranch[0].Branch"))
{
string LastKey = Request.Form.AllKeys.Where(n => n.StartsWith("ThirdPartyBranch[")).Last();
int LastKeyNumber = int.Parse(LastKey.Split('[', ']')[1]) + 1;
for (int b = 0; b < LastKeyNumber; b++)
{
string updatebranch = Request.Form["ThirdPartyBranch[" + b.ToString() + "].Branch"].ToString();
string updateisFlaggedAsRetailer = Request.Form["ThirdPartyBranch[" + b.ToString() + "].isBranchFlaggedAsRetailer"].ToString();
if(updateisFlaggedAsRetailer.Contains("true"))
{
//Do Update to True;
}
else
{
//Do Update to False;
}
}
}
}
答案 0 :(得分:0)
您的代码对我来说很好。要尝试的东西可能是在模型的默认构造函数中初始化两个列表。我之前遇到的问题是模型绑定器不会创建列表,但如果它们不为空则会填充它们。
public class ThirdPartyViewModel
{
public String MessageText { get; set; }
public BranchAddress Address { get; set; }
public String AddressBranchCode { get; set; }
public String FullAddressBlock { get; set; }
public IList<ThirdPartySubUserLine> ThirdPartyUsers { get; set; }
public IList<ThirdPartyBranch> ThirdPartyBranches { get; set; }
public String BrandStyle { get; set; }
public ThirdPartyViewModel()
{
ThirdPartyUsers = new List<ThirdPartySubUserLine>();
ThirdPartyBranches = new List<ThirdPartyBranch>();
}
}