从下拉列表 MVC 中检索 selectedValue

Retrieve selectedValue from dropdown list MVC

如何从视图中的下拉列表控件中检索 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;
        }

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

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

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

如果您没有使用 jQuery,您可以尝试类似的操作:

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

希望对您有所帮助