Asp.Net MVC和模型被发送回客户端

时间:2011-02-15 01:22:56

标签: c# asp.net-mvc

我现在有一个正常的画面。但是我需要在用户点击“添加”按钮的情况下向表中添加项目。我期望发生的是Post动作在模型中添加一些属性到列表<>,然后将更新的模型发送回客户端。因此,用户在控件中输入了值,单击“添加”...完整模型被发送回控制器...控制器读取属性并将它们添加到我的列表<> (这是模型的一部分)。然后,模型将被发送回View,并根据列表中的项目构建a。

目前,我正在尝试这个:

 public ActionResult AccountTransaction()
    {

        List<AccountDto> accounts = Services.AccountServices.GetAccounts(false);
        List<PayeeDto> payees = Services.PayeeServices.GetPayees();
        List<CostCenterDto> costCenters = Services.CostCenterServices.GetCostCenters();
        List<TransactionTypeDto> transactionTypes = Services.TransactionTypeServices.GetTransactionTypes();

        AccountTransactionView v = new AccountTransactionView
                                       {
                                           Accounts = (from a in accounts
                                                       where a.Deleted == false
                                                       select new SelectListItem
                                                                  {
                                                                      Text = a.Description,
                                                                      Value = a.AccountId.ToString(),
                                                                      Selected = false
                                                                  }

                                                      ),
                                           Payees = (from p in payees
                                                     where p.Deleted == false
                                                     select new SelectListItem
                                                                {
                                                                    Selected = false,
                                                                    Text = p.Name,
                                                                    Value = p.PayeeId.ToString()

                                                                }),
                                           TransactionTypes = (from tt in transactionTypes

                                                               select new TransactionTypeView
                                                                          {
                                                                              Deleted = false,
                                                                              Description = tt.Description,
                                                                              Value = tt.TransactionTypeId
                                                                          }).ToList(),

                                           CostCenters = (from cc in costCenters
                                                          where cc.Deleted == false
                                                          select new SelectListItem
                                                                     {
                                                                         Selected = false,
                                                                         Text = cc.Name,
                                                                         Value = cc.CostCenterId.ToString()
                                                                     }),
                                           Deleted = false,
                                           SelectedAccountId = 0,
                                           SelectedCategoryId = 0,
                                           SelectedSubCategoryId = 0,
                                           SelectedBudgetId = 0,
                                           TransactionDate = DateTime.Now
                                       };


        return View(v);
    }

    [HttpPost]
    public ActionResult AccountTransaction(AccountTransactionView model)
    {
        model.TransactionSplitLines.Add(new TransactionSplitLine
                                            {Amount = "100", Category = "Test", SubCategory = "Test More"});
        return View("AccountTransaction", model);
    }

所以,当页面被加载时,'公共ActionResult AccountTransaction()'被调用...并且HttpPost方法被击中......但是...当我返回View时...前端失败,如这个模型现在似乎是空的。视图在这里失败:

            <td align="right">
            <% foreach (var tt in Model.TransactionTypes)
               {%>
            <%=tt.Description %>
            <%=Html.RadioButton("SelectedTransactionTypeId", tt.Value) %><br />
            <%
                }%>
        </td>

突然之间,Model.TransactionTypes现在是Null ......但是,为什么,因为我认为我正在返回相同的模型。

1 个答案:

答案 0 :(得分:1)

整个Model属性null还是TransactionTypes属性?请记住,模型绑定的工作原理是通过post中的数据构建对象。对于TransactionTypeView方法的HttpPost版本中绑定模型中存在的每个AccountTransaction个对象,它们必须出现在您要发送到的form中服务器。 This question讨论了如何绑定对象集合(以及如何构建标记来处理它)。