I have a MVC3 project (using C#) and am using Razor and EntityFramework 4.1 and I'd like to accomplish the following:
- Create a dropdown that displays provinces. Once you choose a province, it automatically populates a second dropdown that shows the cities under that province.
- A second dropdown that shows the cities (as populated by the first dropdown), once the user chooses the city, it will automatically populate a table showing the suburbs under the city.
- A table that contains a list of suburbs (based on the second dropdown).
Thanks in advance!
I've created my models,
public class Provinces
{
[Key]
public int ProvinceID {get; set;}
public string Province {get; set;}
public virtual ICollection<City> City {get;set;}
}
public class City
{
[Key]
public int CityID {get; set;}
public string City {get; set;}
public virtual ICollection<Suburb> Suburb {get; set;}
public virtual Province Province {get; set;}
}
public class Suburb
{
[Key]
public int SuburbID {get; set;}
public string Suburb {get; set;}
public virtual City City {get; set;}
}
A ViewModel to handle the dropdown
public class MyViewModel
{
public string SelectedProvinceID {get; set;}
public string SelectedCityID {get; set;}
public IEnumerable<Province> Province {get; set;}
}
A Controller
public class Suburbs : Controller
{
MyEntities suburbsDB = new MyEntities();
public ActionResult Create()
{
//For Province dropdown
ViewBag.Province = suburbsDB.Province.OrderBy(prov => prov.Province).ToList();
var suburbDetails = new Suburb();
return View(suburbDetails);
}
I'm quite new to EntityFramework, Json, and Razor and I'm having a problem on how will I create the view. I've referenced the answer in Cascading drop-downs in MVC3 Razor view Cascading drop-downs in MVC 3 Razor view
How can I refresh the table based on the value of the Suburb dropdown?