0

How can I retrieve selectedValue from dropdownlist control from within the view (without storing any value in the model)?

Is there any way to do it within the Razor section?

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;
        }
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Heretic Monkey Mar 14 '17 at 16:33

1 Answers1

0

Why do you have the switch statement? Do you need to have an ID on each option element? If not, you can reference the selected element as such:

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

EDIT: You just updated your question as I posted.

If you are not using jQuery, you could try something like:

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

Hope it helps

John Lee
  • 1,357
  • 1
  • 13
  • 26