0

I need to get the value of my @Html.DropdownList for adding a condition by its value. But I don't know how can I get that. What should I do? Here's my code. help me for this...

@using (Html.BeginForm("Index", "Search", routeValues, FormMethod.Get, new RouteValueDictionary { { "class", "search-form" } })) {
    <fieldset>
        @Html.DropDownList("q",new List<SelectListItem>
   {
       new SelectListItem {Text ="تور",Value="تور"},
       new SelectListItem {Text ="ویزا",Value="ویزا"},
       new SelectListItem {Text ="اقامت گاه",Value="اقامت گاه"},
       new SelectListItem {Text ="مولتی",Value="مولتی"},
   },"یک گزینه را انتخاب کنید")
        @Html.DropDownList("secq",new List<SelectListItem>
   {
       new SelectListItem {Text ="انگلستان",Value="انگلستان"},
       new SelectListItem {Text ="ژاپن",Value="ژاپن"},
       new SelectListItem {Text ="روسیه",Value="روسیه"},
       new SelectListItem {Text ="کانادا",Value="کانادا"},
       new SelectListItem {Text ="کنیا",Value="کنیا"},
       new SelectListItem {Text ="سافاری",Value="سافاری"},
       new SelectListItem {Text ="ده روزه",Value="ده روزه"},

   },"یک گزینه را انتخاب کنید")
        @Html.Hidden("culture", WorkContext.CurrentCulture)
        <button type="submit">@T("Search")</button>
    </fieldset>
}

Thank you

Yiyi You
  • 16,875
  • 1
  • 10
  • 22

1 Answers1

0

You can get this dropdown list value in view js like this:

the first dropdown list value:

$("#q").val()

the second dropdown list value:

$("#secq").val()

Update:

Here is a demo:

view:

<fieldset>
        @Html.DropDownList("q", new List<SelectListItem>
   {
       new SelectListItem {Text ="تور",Value="تور"},
       new SelectListItem {Text ="ویزا",Value="ویزا"},
       new SelectListItem {Text ="اقامت گاه",Value="اقامت گاه"},
       new SelectListItem {Text ="مولتی",Value="مولتی"},
   }, "یک گزینه را انتخاب کنید")
        @Html.DropDownList("secq", new List<SelectListItem>
   {
       new SelectListItem {Text ="انگلستان",Value="انگلستان"},
       new SelectListItem {Text ="ژاپن",Value="ژاپن"},
       new SelectListItem {Text ="روسیه",Value="روسیه"},
       new SelectListItem {Text ="کانادا",Value="کانادا"},
       new SelectListItem {Text ="کنیا",Value="کنیا"},
       new SelectListItem {Text ="سافاری",Value="سافاری"},
       new SelectListItem {Text ="ده روزه",Value="ده روزه"},

   }, "یک گزینه را انتخاب کنید")

    </fieldset>
<button onclick="getdata()">getdata</button>


<script>
    function getdata() {
        var q = $("#q").val();
        var secq = $("#secq").val();
    }
</script>

result: enter image description here

Update2: view(when the first dropdown value == "تور",add a option text with test to the second dropdown list):

<fieldset>
    @Html.DropDownList("q", new List<SelectListItem>
{
   new SelectListItem {Text ="تور",Value="تور"},
   new SelectListItem {Text ="ویزا",Value="ویزا"},
   new SelectListItem {Text ="اقامت گاه",Value="اقامت گاه"},
   new SelectListItem {Text ="مولتی",Value="مولتی"},
}, "یک گزینه را انتخاب کنید")

    @Html.DropDownList("secq", new List<SelectListItem>
{
   new SelectListItem {Text ="انگلستان",Value="انگلستان"},
   new SelectListItem {Text ="ژاپن",Value="ژاپن"},
   new SelectListItem {Text ="روسیه",Value="روسیه"},
   new SelectListItem {Text ="کانادا",Value="کانادا"},
   new SelectListItem {Text ="کنیا",Value="کنیا"},
   new SelectListItem {Text ="سافاری",Value="سافاری"},
   new SelectListItem {Text ="ده روزه",Value="ده روزه"},

}, "یک گزینه را انتخاب کنید")

</fieldset>


@section scripts{
    <script>
        $("#q").on("change", function () {
            var q = $("#q").val();
            if (q == "تور") {
               
                $('#secq').append(`<option value="test"> 
                                       test
                                  </option>`); 
            }
        });

    </script>

}

result: enter image description here

Yiyi You
  • 16,875
  • 1
  • 10
  • 22