开发者

ASP.Net MVC returning value of a DropDown

I am learning, and managed to make a nice info display page, displaying lists of transactions. However, I have no added DropDown box, which has a list of Bank Accounts. It's defaulted to All.

When the user selects an account, and presses the submit button, the page should then reloca with only transactions for that account.

I have created the drop down, and the form for it, like this:

    <form id="form1" runat="server">
<h2>
    Transactions:</h2>
<p>
    <%
        using (Html.BeginForm())
        {
    %>
    Bank Account:
    <% =Html.DropDownList("ddAccounts", Model.BankAccountItems)%> <input id="Submit1" type="submit" value="Select" />     
    <%
        }
    %>
</p>
</form>

My model contains this:

    public class AccountTransactionDisplay
{
    public AccountTransactionDisplay()
    {
        DisplayLines = new List<AccountTransactionDisplayLine>();
    }

    public SelectList BankAccountItems { get; set; }
    public List<AccountTransactionDisplayLine> DisplayLines {get; set; }
    public string ClosingBalance { get; set; }
}

public class BankAccountDropdownItem
{
    public string Text {get; set;}   
    public string Value {get; set;}
}

public class AccountTransactionDisplayLine
{
    public string RowColouring { get; set; }
    public string TransactionDate { get; set;}
    public string PayeeName { get; set; }
    public string Amount { get; set; }
    public bool AmountIsDebit { get; set; }
    public string CategoryName { get; set; }
    public string CostCenterName { get; set; }
    public string BudgetName { get; set; }
    public string RunningTotal { get; set; }
    public bool RunningTotalIsDebit { get; set; }
    public bool AlternateRowColour { get; set; }
    public bool HasSplitItems { get; set; }
}

So, AccountTransactionDisplay is the model I pass to the view. In that model, I have this:

 public SelectList BankAccountItems { get; set; }

That holds a list of items that are displayed in my Drop Down. That is being displayed correctly.

However, when the user clicks the submit button,开发者_如何学Go I am unsure how to get the selected value back.

I thought that I would have a method in my controller that accepts the POST... So, I have added this:

        [HttpPost]
    public ActionResult Transactions(AccountTransactionDisplay model)
    {
        AccountTransactionDisplay trans = GetTransactions();
        return View(trans);
    }

And if I set a breakpoint, it is hitting this, but it seems model is empty. I need to add the id of the selected account ot the GetTransactions method, to do the filtering.


When your page renders, view the source. Whatever the name attribute of the select element is, that is the name of a property you will need to add to your model, then the model binder will bind that value to your model. When a form is submitted, only name/value pair of the form fields are submitted (for the most part), so the select element's name will be in the form data as well as its selected value, all of the options are not posted back, so MVC has no way of rebinding your SelectList property.

HTH

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜