开发者

Asp.Net MVC and models being sent back to client

I have a screen that is being drawn fine at the moment. But I need to add items to a table with the user clicks an 'Add' button. What I expect to happen is that the Post action adds a few properties in the model, to a list<>, and then sends the updated model back to the client. So, the user has entered a value in a control, clicks 'Add'... the full model gets sent back to the controller... the controller reads the properties and adds them to my List<> (which is part of the model). The model then gets sent back to the View, and a is built, based on items in the list.

At the moment, I am trying this:

 public ActionResult AccountTransaction()
    {

        List<AccountDto> accounts = Services.AccountServices.GetAccounts(false);
        List<PayeeDto> payees = Services.PayeeServices.GetPayees();
        List<CostCenterDto> costCenters = Services.CostCenterServices.GetCostCenters();
      开发者_Python百科  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);
    }

So, ' public ActionResult AccountTransaction()' is being called when the page is loaded... And the HttpPost method is being hit... but ... when I return View... the front end fails, as the model seems to be empty now. The view fails here:

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

And this is being suddenly, Model.TransactionTypes is now Null... But, why, as I thought I was returning the same model.


Is the entire Model property null or just the TransactionTypes property? Remember that model binding works by building the objects from the data in a post. For each of the TransactionTypeView objects to be present in the binded model in the HttpPost version of the AccountTransaction method, they need to be present in the form you are sending to the server. This question discusses how to bind a collection of objects (and how to construct your markup to handle it).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜