This is the first time im posting a question here. I am new to mvc. I want to develop two cascading dropdown lists. I am using mvc4. Here what i have done.
Factory class
public class Factory
{
[Key]
public int FactoryId { get; set; }
[Required(ErrorMessage = "Required")]
public string FactoryCode { get; set; }
[Required(ErrorMessage = "Required")]
public string FactoryName { get; set; }
public int City { get; set; }
public int Country { get; set; }
I have Country and City classes separately with a foreign key relationship
City class
public class City
{
[Key]
public int CityId { get; set; }
[Required(ErrorMessage = "Required")]
public string CityCode { get; set; }
[Required(ErrorMessage = "Required")]
public string CityName { get; set; }
[ForeignKey("Country")]
public int CountryId { get; set; }
public virtual Country Country { get; set; }
}
Country class
public class Country
{
[Key]
public int CountryId { get; set; }
[Required(ErrorMessage = "Required")]
public string CountryCode { get; set; }
[Required(ErrorMessage = "Required")]
public string CountryName { get; set; }
public virtual List<City> cities { get; set; }
}
Factory controller
public ActionResult FactoryIndex(string A, int? Id, string sortOrder, string currentFilter, string searchString, int? page)
{
var objContext = new KnittingdbContext();
if (A == "New")
{
var model = new Factory();
ViewData["mdl"] = model;
ViewBag.CountryList = objContext.Countries;
ViewBag.Module = A;
}
.....................
Rendering the partialview within the IndexView
@if (ViewBag.Module != null)
{
if (ViewBag.Module == "New")
{
Html.RenderPartial("~/Areas/Masters/Views/Factory/_FactoryCreate.cshtml", ViewData["mdl"]);
}
_FactoryCreate partial view
@Html.DropDownListFor(a=>a.Country,new SelectList(ViewBag.CountryList, "CountryId", "CountryName"), "Select country", new {id="CountryId", @class = "form-control"})
@Html.DropDownListFor(a => a.City; a.City, new SelectList(Enumerable.Empty<SelectListItem>(), "CityId", "CityName"), "Select city", new {@class = "form-control" })
jquery script within the partialview
$(function() {
$('#CountryId').change(function () {
$.ajax({
url: '/Factory/FillCity/',
type: "GET",
dataType: "JSON",
data: { Country: countryId },
success: function (cities) {
$("#City").html("");
$.each(cities, function (i, city) {
$("#City").append(
$('<option></option>').val(city.CityId).html(city.CityName));
});
}
});
})
})
ActionResult in FactoryController that fetches the relevant City data
public ActionResult FillCity(int country)
{
var db = new KnittingdbContext();
var cities = db.Cities.Where(c =>c.CountryId == country);
return Json(cities, JsonRequestBehavior.AllowGet);
}
Country dropdownlist is working.
But the City DDL is not working. The data doesn't get binded.
When the CountryId gets changed it comes to jquery script. i could recognize it using a Alert. But after the URL property in script it doesn't work.
Success method is not getting executed. I think the issue is with the way i present the url. I tried it with different ways. But still couldn't get it solved. These views and controllers are in a Area.
Please help me. Thanks in advance!
countryIdsocountryin the controller would be null.City(as returned bydb.Cities) - may as well fix them all for you :)