繁体   English   中英

ASP.Net MVC的DropDown返回值

[英]ASP.Net MVC returning value of a DropDown

我正在学习,并设法制作了一个不错的信息显示页面,显示了交易列表。 但是,我没有添加DropDown框,该框具有银行帐户列表。 默认为全部。

当用户选择一个帐户并按下“提交”按钮时,页面应仅与该帐户的交易相关。

我已经创建了下拉列表及其表单,如下所示:

    <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>

我的模型包含以下内容:

    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; }
}

因此,AccountTransactionDisplay是我传递给视图的模型。 在那个模型中,我有这个:

 public SelectList BankAccountItems { get; set; }

那保存了我的下拉菜单中显示的项目列表。 显示正确。

但是,当用户单击“提交”按钮时,我不确定如何取回所选值。

我以为我的控制器中会有一个可以接受POST的方法...因此,我添加了以下内容:

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

如果我设置了一个断点,它将达到目标,但模型似乎是空的。 我需要在GetTransactions方法中添加所选帐户的ID,以进行过滤。

页面呈现后,查看源。 无论select元素的name属性是什么,即您将需要添加到模型的属性的名称,模型绑定器都会将该值绑定到模型。 提交表单时,仅提交表单字段的名称/值对(大部分情况下),因此select元素的名称及其所选值将在表单数据中,所有选项都不会回发,因此MVC无法重新绑定SelectList属性。

高温超导

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM