Maryam
Maryam

Reputation: 1071

check the selected value in drop down list

I have the following drop down list. How can I check the selected value BEFORE saving the data into the module? I want to use the selected value in setting the values of Neighborhood's drop down list.

        @Html.DropDownListFor(model => model.City, new SelectList(
          new List<Object>{
               new { value = "Abha", text = "Abha" },
               new { value = "Al Qunfudhah", text = "Al Qunfudhah" },
               new { value = "Al-Kharj", text = "Al-Kharj" },
               new { value = "Al-Ahsa", text = "Al-Ahsa" },
               new { value = "Buraidah", text = "Buraidah" },
               new { value = "Dammam", text = "Dammam" },
               new { value = "Ha'il", text = "Ha'il"},
               new { value = "Hafar Al-Batin", text = "Hafar Al-Batin" },
               new { value = "Jazan", text = "Jazan" },
               new { value = "Jeddah", text = "Jeddah" },
               new { value = "Jubail", text = "Jubail" },
               new { value = "khobar", text = "khobar" },
               new { value = "Khamis Mushait", text = "Khamis Mushait" },
               new { value = "Mecca", text = "Mecca" },
               new { value = "Medina", text = "Medina" },
               new { value = "Najran", text = "Najran" },
               new { value = "Qatif", text = "Qatif" },
               new { value = "Riyadh", text = "Riyadh" },
               new { value = "Tabuk", text = "Tabuk" },
               new { value = "Ta'if", text = "Ta'if" },
               new { value = "Yanbu", text = "Yanbu" }
          },"value", "text", "Jeddah"))

        @Html.ValidationMessageFor(model => model.City)
    </div>
</div> 

Upvotes: 0

Views: 675

Answers (1)

AmmarCSE
AmmarCSE

Reputation: 30557

With jQuery, you can try

$('#City').val();

If you are not using jQuery, the vanilla javascript way would be

var element = document.getElementById("City");
var strVal = element.options[element.selectedIndex].value;

Upvotes: 1

Related Questions