Click here to Skip to main content
15,899,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends

I want to bind dropdownlist in asp.net MVC

I have 2 dropdowns one is country 1 is State I want to Bind this 2 dropdowns using Entity Frame work i want to bind.

After Selecting Country i want to bind state Names Dropdownlist please help me how to do this on Asp.net MVC4..


Thanks :
Ajay
Posted

Please check on same url

http://www.a2zmenu.com/Blogs/MVC/How-to-fill-DropdownList-control-and-SelectedIndexChanged-event-in-MVC.aspx
 
Share this answer
 
Comments
jo.him1988 7-Jul-14 8:53am    
hi here is the solution i am newbie in mvc let she if its help you :)
@{
 ViewBag.Title = "Main";
}
<script type="text/javascript" src="../../Scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
    function GetState() { 
    var selectedValue = $('#ddlCountry').val();
        window.location.href = '/Bind/GetStatesByID/?countryCode=' + selectedValue;
    }
</script>
<h2>Main</h2>
@using (Html.BeginForm())
{

    @Html.DropDownList("Country", (SelectList)ViewBag.countryList, "--Choose Your Country--", new { id = "ddlCountry",onchange="GetState();" })
    @Html.DropDownList("State", (SelectList)ViewBag.stateList, "--Choose Your State--", new { id = "ddlState" })
}



public class BindController : Controller
   {
       //
       // GET: /Bind/

       public ActionResult Index()
       {
           ViewBag.countryList = GetAllCountrys();
           ViewBag.stateList = GetAllStates();
          return View("Main");
       }
       public ActionResult GetStatesByID(int? countryCode)
       {
           ViewBag.stateList = GetAllStatsByID(countryCode);
           ViewBag.countryList = new SelectList(GetAllCountrys(), "Value", "Text", countryCode);
           return View("Main");
       }

       private SelectList GetAllStatsByID(int? countryCode)
       {
           Data.TestEntities entity = new Data.TestEntities();
           {
               IEnumerable<SelectListItem> selectedStates = entity.States.Where(s => s.CountryID == countryCode).AsEnumerable().Select(m => new SelectListItem()
               {
                   Text = m.StateName
                   ,
                   Value = m.StateID.ToString()
               });
               return new SelectList(selectedStates, "Value", "Text", "--Select State--");
           }
       }
       private SelectList GetAllCountrys()
       {
           Data.TestEntities entity = new Data.TestEntities();
           {
               IEnumerable<SelectListItem> country = entity.Countries.AsEnumerable().Select(m => new SelectListItem()
               {
                   Text = m.CountryName
                   ,
                   Value = m.CountryID.ToString()
               });
               return new SelectList(country, "Value", "Text","--Select Country--");
           }

       }

       private SelectList GetAllStates()
       {
           Data.TestEntities entity = new Data.TestEntities();
           {
               IEnumerable<SelectListItem> states = entity.States.AsEnumerable().Select(m => new SelectListItem()
               {
                   Text = m.StateName
                   ,
                   Value = m.StateID.ToString()
               });
               return new SelectList(states, "Value", "Text", "--Select States--");
           }
       }
   }
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900