ASP.Net MVC 返回 DropDown 的值

发布于 2024-10-12 22:46:51 字数 2177 浏览 1 评论 0原文

我正在学习,并设法制作了一个漂亮的信息显示页面,显示交易列表。但是,我没有添加下拉框,其中包含银行帐户列表。默认为全部。

当用户选择一个帐户并按下提交按钮时,页面应该重新定位,仅包含该帐户的交易。

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

    <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 来进行过滤。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最偏执的依靠 2024-10-19 22:46:51

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

华泰

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文