2015-12-21 47 views
0

我有一个MVC页面正在做一些奇怪的事情。我有一个包含两个IList,分支和用户的类。IList's not being发布返回视图模型

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; } 
 
}

该视图包含2个选项卡和2个EditorFor使用模板

<div id="tabContainer" class="container"> 
 
    <ul class="tabs"> 
 
\t  <li class="tab-link current" data-tab="tab1">Users</li> 
 
\t  <li class="tab-link" data-tab="tab2">Branches</li> 
 
    </ul> 
 
    
 
     <div id="tab1" class="tab-content current"> 
 
\t  <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"> 
 
\t  <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"])"> 
 
\t <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> 
 
\t <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> 
 
\t <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> 
 
\t <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"])"> 
 
\t <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> 
 
\t <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> 
 
\t <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> 
 
\t <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 
 
}

在控制器方面, '保存' 的ActionResult下,我并不总是得到双方的IList回来。这是我所知道的:

  1. 更改数据库不有所作为
  2. 节省用户总是工作
  3. 保存分行只适用时,用户的数量比数 相同或更低分行
  4. 删除了用户的“editorfor”允许分行保存,甚至当更多的用户存在
  5. 有比分支机构较少的用户,该用户仍然保存
  6. 的交换两个“editorfor”周围没什么区别
  7. 在视图模型周围交换两个ILIST没什么区别
  8. 我试图使双方的编辑模板不同,加入在某些地方,一些 “2”,没有什么区别

我希望我已经提供了足够的信息给有人发现我犯过的错误,但如果不是,请询问。我希望这是愚蠢的。我一直在看这太久。有任何想法吗?




UPDATE:

花了大昨天看这个。我连接了Fiddler并观看了回来的内容。在所有情况下,这两个IList的详细信息都会被回传,但是当您将鼠标悬停在vs2013的回发视图模型上时,分支IList在特定条件下为空。如果你看看'Request.Form',你可以每次看到结果,所以我做了一个Dirty Hack(见下)。通过查看Fiddler,我也注意到'CheckBoxFor'控件返回'False'或'False,True',所以我处理了这个。

仍然引发问题为什么?
​​

回答

0

您的代码对我来说很好。要尝试的东西可能是初始化模型默认构造函数中的两个列表。在模型绑定器不会创建列表的位置之前,我遇到了一个问题,但如果它们不为null,则会填充它们。

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>(); 
    } 
} 
+0

我总是会在屏幕上显示2列表,但我并不总是将两个列表都发回来。谢谢 – Homerml

+0

检查生成的html是否有正确的属性名称,例如:ThirdPartyUsers [0] .UserId。我的另一个问题是,在提交之前禁用输入字段时,它们不包含在发布数据中。但是我看不到你是否禁用任何物品。 –

+0

只是看看'查看源代码'。对不起没有问题。一切都是唯一的,而不是禁用的 – Homerml