raja ramesh

raja ramesh

  • NA
  • 50
  • 70.9k

How to select html dropdownlist default value in mvc3 using jquery

Jan 10 2012 8:49 AM
Hi,

I am working on Asp.net mvc3

My view part

<select name="" class="styled" id="countryDropDown" >            
@foreach(var country in HttpContext.Current.Cache.Get("Countries") as List<Country>){
<option value="@country.Id" >@country.Name</option>
}
</select>
<select name="" class="styled" id="stateDropDown" > </select>  
I have written following code to cascading dropdownlists 
opulateStatesDropDown()
{
//code for populate statedropdowlist and it is working
}
 $("#countryDropDown").val("@ViewBag.CountryID"); //This line is working
    if ($("#countryDropDown").val() != null) { 
        PopulateStatesDropDown();      
        $("#stateDropDown").val("@ViewBag.StateID"); //This is not working

control Action

ViewBag.CountryID = Country.Id;
ViewBag.StateID = State.Id;
I have written above code for select default values and its working for Countrydropdownlist but its not working for statedropdownlist.

Can you tel me anyone where I have done mistake ?

Thank you in advance

Answers (3)