Asp.Net MVC和正在发送回客户端的模型

问题描述:

我有一个屏幕,此刻正在绘制好。但我需要添加项目到一个表中,用户点击一个'添加'按钮。我期望发生的事情是Post操作在模型中添加了一些属性,并将其添加到列表<>中,然后将更新后的模型发送回客户端。因此,用户在控件中输入了一个值,点击'添加'...完整的模型被发送回控制器...控制器读取属性并将它们添加到我的列表<>(它是模型)。然后模型被发送回视图,并且基于列表中的项目构建一个。Asp.Net MVC和正在发送回客户端的模型

此刻,我想这一点:

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现在是空......可是,为什么,因为我以为我回到同一型号。

整个Model属性null或只是TransactionTypes属性?请记住,模型绑定的工作原理是从post中的数据构建对象。对于TransactionTypeView中的每个TransactionTypeView对象在AccountTransaction方法的HttpPost版本中的绑定模型中都存在,它们需要存在于要发送到服务器的form中。 This question讨论如何绑定一组对象(以及如何构建标记来处理它)。

+0

谢谢。当我找回它时,该模型不为空...但我在第一个方法中为模型(transactionTypes)创建的属性全为空。然而,值回发(日期编辑框..等)在那里。它看起来像我需要再次建立这些List值? – Craig 2011-02-15 02:41:50