Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to bind a dynamic dropdown list

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 369
    Comment on it

    In this blog we illustrate how to bind a dynamic dropdown list when dropdown list is coming from a databse in MVC

    See the below Example code:

    We bind a dropdownList for a States where the list of states is coming from a database.
     
    We make StateDataController

     

     public class StateDataController : Controller
        {
            string connection = ConfigurationManager.ConnectionStrings["OrgEntity"].ConnectionString;
            public ActionResult Index()
            {
                try
                {
                    State obj = new State();
                    using (SqlConnection conn = new SqlConnection(connection))
                    {
                        conn.Open();
    
                        List<SelectListItem> StateList = new List<SelectListItem>();
                        var cmdState = new SqlCommand("usp_State", conn);                                           //usp_state is Stored Procedure
                        cmdState.CommandType = CommandType.StoredProcedure;
                        if (cmdState != null)
                        {
                            SqlDataReader StateReader = null;
                            StateReader = cmdState.ExecuteReader();
                            while (StateReader.Read())
                            {
                                StateList.Add(new SelectListItem { Text = StateReader["StateName"].ToString(), Value = StateReader["StateID"].ToString() });
                            }
                            obj.StateList = StateList;
                            StateReader.Close();
                        }      
                  }
           }

     

    We make a class State

    public class State
        {
            public int StateID { get; set; }
            public string StateName { get; set; }
            public IEnumerable<SelectListItem> StateList { get; set; }
    
        }
    
    

     

     

    View to display the dropdown list

    @model EmployeeData.Models.State
    
    <div>
         @Html.DropDownListFor(model => model.StateID, Model.StateList, "--Select--")                
    </div>

     

     

    Conclusion:-

    In above example we make a connection with sql server database where data for dropdown will come then we make store procedure in sql server to get the StateList. After getting the StateList we bind the dropdown with StateList. After binding a StateList we make a view to display the dynamic binded dropdown list.


     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: