vikas gupta

vikas gupta

  • NA
  • 22
  • 0

find dropdown list selected index value(using MVC)

Apr 7 2011 9:02 AM

i have a dropdown of country.i want to get selected country id and on its behalf want to bid state drop down

 

my Controller code is:

 

DataTable dt = new DataTable();
            dt = cs.GetCountry();
            List<SelectListItem> iliSLI = new List<SelectListItem>();
            if (dt.Rows.Count > 0)
            {
                for (Int32 i = 0; i <dt.Rows.Count; i++)
                {

                    SelectListItem sli = new SelectListItem();
                    sli.Text = dt.Rows[i]["Name"].ToString();
                    sli.Value = dt.Rows[i]["id"].ToString();
                    sli.Selected = true;
                    iliSLI.Add(sli);

                }
            }
            ViewData["SecretQuestion"] = iliSLI;
         

 

view code is:

 

  <%= Html.DropDownList("SecretQuestion") %>

 

Models code is:

 public DataTable GetCountry()
        {
            SqlConnection con;
            con = new SqlConnection( ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            SqlDataAdapter adp = new SqlDataAdapter("select * from CountryTable", con);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            return dt;

        }


Answers (1)