Click here to Skip to main content
15,885,665 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to create cascading dropdown lists in MVC4 using Database first approach
Posted

Under your controller you should try this, I think it will help you.

public ActionResult Provinces()

{
//Here i'm creating a generic list and I am adding an item to it

List<selectlistitem> prov = new List<selectlistitem>();
prov .Add(new SelectListItem { Text = "Select Province", Value = "0" });
prov .Add(new SelectListItem { Text = "Northern Cape", Value = "1" });
prov .Add(new SelectListItem { Text = "Eastern Cape", Value = "2" });
prov .Add(new SelectListItem { Text = "Free State", Value = "3" });
prov .Add(new SelectListItem { Text = "Western Cape", Value = "4" });
prov .Add(new SelectListItem { Text = "Limpopo", Value = "5" });
prov .Add(new SelectListItem { Text = "North West", Value = 6});
prov .Add(new SelectListItem { Text = "KwaZulu-Natal", Value = "7" });
prov .Add(new SelectListItem { Text = "Mpumalanga", Value = "8" });
prov .Add(new SelectListItem { Text = "Gauteng", Value = "9" });

//storing provinces in ViewData it will pass them to the view
ViewData["province"] = prop;
return View();
}
// Now you can add your View by right-click on ActionResults method Provinces and you have to select Add View

@using (Html.BeginForm())
{
@Html.DropDownList("Province", ViewData["province"]as List<selectlistitem>)
}
 
Share this answer
 

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