title: Populate a drop down list based on another drop down list selected value in MVC 4
body: <p>I have referred <a href="https://stackoverflow.com/questions/29675636/how-to-populate-a-dropdownlist-based-on-another-dropdownlist-selected-value-in-m">This ans</a> of stackoverflow It is working fine but since i have taken drop down list in forms so when second dropdown list gets populate selected value in first dropdown list is getting effected. How to maintain that value??</p>
  <p>Here is my code.</p>
  <p><strong>View</strong></p>
  <pre><code>@{ Html.BeginForm(&quot;Create&quot;, &quot;User&quot;, FormMethod.Post, new { enctype = &quot;multipart/form-data&quot; }); }
  @Html.DropDownList(&quot;country&quot;, ViewData[&quot;Id&quot;] as List&lt;SelectListItem&gt;, new { onchange = &quot;this.form.submit();&quot; })
  @{ Html.EndForm(); }
  
  
  @{ Html.BeginForm(&quot;Create&quot;, &quot;User&quot;, FormMethod.Post, new { enctype = &quot;multipart/form-data&quot; }); }
  @Html.DropDownList(&quot;state&quot;, ViewData[&quot;Id1&quot;] as List&lt;SelectListItem&gt;, new { onchange = &quot;this.form.submit();&quot; })
  @{ Html.EndForm(); }
  
  
  @{ Html.BeginForm(&quot;Create&quot;, &quot;User&quot;, FormMethod.Post, new { enctype = &quot;multipart/form-data&quot; }); }
  @Html.DropDownList(&quot;city&quot;, ViewData[&quot;Id2&quot;] as List&lt;SelectListItem&gt;, new { onchange = &quot;this.form.submit();&quot; })
  @{ Html.EndForm(); }
  </code></pre>
  <p><strong>Controller</strong></p>
  <pre><code> public ActionResult Create()
      {
          FillCountry();
          FillState();
          FillCity();
          return View();
      }
      [HttpPost]
      public ActionResult Create(User ur)
      {
          string str = @&quot;Data Source=DEV_3\SQLEXPRESS;Initial Catalog=DB_Naved_Test;Integrated Security=True&quot;;
          SqlConnection con = new SqlConnection(str);
          string query = &quot;Insert into tblTest (Name,Email,MobileNo) values('&quot; + ur.Name + &quot;','&quot; + ur.Email + &quot;','&quot; + ur.MobileNo + &quot;')&quot;;
          con.Open();
          SqlCommand cmd = new SqlCommand(query, con);
          cmd.ExecuteNonQuery();
          con.Close();
          TempData[&quot;msg&quot;] = &quot;&lt;script&gt;alert('Inserted Successfully');&lt;/script&gt;&quot;;
          ModelState.Clear();
          FillCountry();
          FillState();
          FillCity();
          return View();
  
  
      }
      public void FillCountry()
      {
          string str = @&quot;Data Source=DEV_3\SQLEXPRESS;Initial Catalog=DB_Naved_Test;Integrated Security=True&quot;;
          SqlConnection con = new SqlConnection(str);
          string query = &quot;select * from tblCountry &quot;;
          SqlCommand cmd = new SqlCommand(query, con);
          con.Open();
          SqlDataReader rdr = cmd.ExecuteReader();
          List&lt;SelectListItem&gt; li = new List&lt;SelectListItem&gt;();
          li.Add(new SelectListItem { Text = &quot;Select&quot;, Value = &quot;0&quot; });
          while (rdr.Read())
          {
              li.Add(new SelectListItem { Text = rdr[1].ToString(), Value = rdr[0].ToString() });
          }
         
          ViewData[&quot;Id&quot;] = li;
          
      }
      public void FillState()
      {
          int id = Convert.ToInt16(Request[&quot;country&quot;]);
          string str = @&quot;Data Source=DEV_3\SQLEXPRESS;Initial Catalog=DB_Naved_Test;Integrated Security=True&quot;;
          SqlConnection con = new SqlConnection(str);
          string query = &quot;select * from tblState where cid='&quot; + id + &quot;'&quot;;
          SqlCommand cmd = new SqlCommand(query, con);
          con.Open();
          SqlDataReader rdr = cmd.ExecuteReader();
          List&lt;SelectListItem&gt; li = new List&lt;SelectListItem&gt;();
          li.Add(new SelectListItem { Text = &quot;Select&quot;, Value = &quot;0&quot; });
          while (rdr.Read())
          {
              li.Add(new SelectListItem { Text = rdr[1].ToString(), Value = rdr[0].ToString() });
          }
          ViewData[&quot;Id1&quot;] = li;
      }
      public void FillCity()
      {
          int id = Convert.ToInt16(Request[&quot;state&quot;]);
          string str = @&quot;Data Source=DEV_3\SQLEXPRESS;Initial Catalog=DB_Naved_Test;Integrated Security=True&quot;;
          SqlConnection con = new SqlConnection(str);
          string query = &quot;select * from tbl_cities where StateId='&quot; + id + &quot;'&quot;;
          SqlCommand cmd = new SqlCommand(query, con);
          con.Open();
          SqlDataReader rdr = cmd.ExecuteReader();
          List&lt;SelectListItem&gt; li = new List&lt;SelectListItem&gt;();
          li.Add(new SelectListItem { Text = &quot;Select&quot;, Value = &quot;0&quot; });
          while (rdr.Read())
          {
              li.Add(new SelectListItem { Text = rdr[1].ToString(), Value = rdr[0].ToString() });
          }
          ViewData[&quot;Id2&quot;] = li;
      }
  </code></pre>
  <p>and also when I am using only two dropdownlist i.e <code>ddlCountry</code> and <code>ddlState</code> and select Country from <code>ddlCountry</code> my <code>ddlSates</code> is getting populated properly but the selected Country from <code>ddlCountry</code> is getting changed.</p>