从下拉列表MVC中检索selectedValue

时间:2017-03-14 16:27:11

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

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

在Razor部分有没有办法做到这一点?

mycode的:

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

1 个答案:

答案 0 :(得分:0)

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

$(“#IDofDropDownList选项:已选择”)。text()

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

如果您不使用jQuery,可以尝试类似:

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

希望有所帮助