2011-02-15 87 views
0

我有一個屏幕,此刻正在繪製好。但我需要添加項目到一個表中,用戶點擊一個'添加'按鈕。我期望發生的事情是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現在是空......可是,爲什麼,因爲我以爲我回到同一型號。

回答

1

整個Model屬性null或只是TransactionTypes屬性?請記住,模型綁定的工作原理是從post中的數據構建對象。對於TransactionTypeView中的每個TransactionTypeView對象在AccountTransaction方法的HttpPost版本中的綁定模型中都存在,它們需要存在於要發送到服務器的form中。 This question討論如何綁定一組對象(以及如何構建標記來處理它)。

+0

謝謝。當我找回它時,該模型不爲空...但我在第一個方法中爲模型(transactionTypes)創建的屬性全爲空。然而,值回發(日期編輯框..等)在那裏。它看起來像我需要再次建立這些List值? – Craig 2011-02-15 02:41:50