【问题标题】:Retrieve selectedValue from dropdown list MVC从下拉列表 MVC 中检索 selectedValue
【发布时间】:2017-03-14 16:27:11
【问题描述】:

如何从视图内的下拉列表控件中检索selectedValue(不在模型中存储任何值)?

在 Razor 部分有什么办法吗?

我的代码:

<script type="text/javascript">

            function dada(aa) {
                 return document.getElementById(aa).value;
            }
    </script>

    @using (Html.BeginForm("MonetaryTransactions", "Trading"))
    {


        IEnumerable<Mt4Transaction> results = from result in Model
                                              select result;

        // This is the dropDown control which I need to get the selectedValue from ...
            @Html.DropDownList("TransactionType",
    new SelectList(Enum.GetValues(typeof(Forex.Reports.ReportValueType))),
    "Transaction Type",
    new { @class = "form-control", @id = "aa" });


        switch ("???")
        {

            case "Withdrawal":
                {
                    results =
                   from result in Model
                   where result.Profit > 0
                   select result;
                }
                break;
            case "Deposit":
                {
                    results =
                      from result in Model
                      where result.Profit < 0
                      select result;
                }
                break;
            case "UnidentifiedProfit":
                {
                    results =
                     from result in Model
                     select result;
                }
                break;
        }

【问题讨论】:

标签: .net asp.net-mvc razor html.dropdownlistfor


【解决方案1】:

为什么会有 switch 语句?您是否需要在每个选项元素上都有一个 ID?如果没有,您可以这样引用所选元素:

$("#IDofDropDownList option:selected").text()

编辑:您刚刚更新了我发布的问题。

如果你不使用 jQuery,你可以试试这样的:

function dada(IDofDropDownList) {
    var ddlist = document.getElementById(IDofDropDownList);
    return ddlist.options[ddlist.selectedIndex].value;
}

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 2017-06-16
    相关资源
    最近更新 更多