Please help, I am trying to implement a cascading dropdownlist on my app, but it does not work. Please help me what is it that I am not doing right here:
My controller
My controller
namespace TelerikMvcDropdownMunicipality.Controllers
{
using System.Linq;
using System.Web.Mvc;
using TelerikMvcDropdownMunicipality.Models;
using System.Data;
using System.Data.Entity;
public partial class MunicipalController : Controller
{
public ActionResult Index()
{
return View();
}
public JsonResult GetCascadeDistricts()
{
var municipality = new MunicipalityEntities();
return Json(municipality.Districts.Select(d => new { DistrictId = d.DistrictId, DistrictName = d.DistrictName }), JsonRequestBehavior.AllowGet);
}
public JsonResult GetCascadeLocals(int? districts)
{
var municipality = new MunicipalityEntities();
var locals = municipality.Locals.AsQueryable();
if (districts != null)
{
locals = locals.Where(l => l.DistrictId == districts);
}
return Json(locals.Select(l => new { LocalId = l.LocalId, LocalMunName = l.LocalName }), JsonRequestBehavior.AllowGet);
}
public JsonResult GetCascadeTowns(int? locals)
{
var municipality = new MunicipalityEntities();
var towns = municipality.Towns.AsQueryable();
if (locals != null)
{
towns = towns.Where(t => t.LocalId == locals);
}
return Json(towns.Select(t => new { TownId = t.TownId, TownName = t.TownName }), JsonRequestBehavior.AllowGet);
}
}
}
and my view is:
@{
ViewBag.Title = "Index";
}
<
div
class
=
"demo-section"
style
=
"width: 250px;"
>
<
h2
>Municipalities</
h2
>
<
p
>
<
label
for
=
"districts"
> Districts</
label
>
@(Html.Kendo().DropDownList()
.Name("districts")
.HtmlAttributes(new { style = "width:200px" })
.OptionLabel("Select district...")
.DataTextField("DistrictName")
.DataValueField("DistrictId")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetCascadeDistricts", "ComboBox");
});
})
)
</
p
>
<
p
>
<
label
for
=
"locals"
>Locals</
label
>
@(Html.Kendo().DropDownList()
.Name("locals")
.HtmlAttributes(new { style = "width:200px" })
.OptionLabel("Select local...")
.DataTextField("LocalName")
.DataValueField("LocalId")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetCascadeLocals", "ComboBox")
.Data("filterLocals");
})
.ServerFiltering(true);
})
.Enable(false)
.AutoBind(false)
.CascadeFrom("districts")
)
<
script
>
function filterLocals() {
return {
districts: $("#districts").val()
};
}
</
script
>
</
p
>
<
p
>
<
label
for
=
"towns"
>Towns</
label
>
@(Html.Kendo().DropDownList()
.Name("towns")
.HtmlAttributes(new { style = "width:200px" })
.OptionLabel("Select towns")
.DataTextField("TownName")
.DataValueField("TownId")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetCascadeTowns", "ComboBox")
.Data("filterTowns");
})
.ServerFiltering(true);
})
.Enable(false)
.AutoBind(false)
.CascadeFrom("towns")
)
<
script
>
function filterTowns() {
return {
towns: $("#filterTowns").val()
};
}
</
script
>
</
p
>
</
div
>
<
script
>
$(document).ready(function () {
var districts = $("#districts").data("kendoDropDownList"),
locals = $("#localMun").data("kendoDownList"),
towns = $("#towns").data("kendoDropDownList");
$("#get").click(function () {
var districtInfo = "\nDistricts: { id: " + districts.value() + ", name: " + districts.text() + " }",
localInfo = "\nLocal: { id: " + locals.value() + ", name: " + locals.text() + " }",
townInfo = "\nTown: { id: " + towns.value() + ", name: " + towns.text() + " }";
alert("Town details:\n" + districtInfo + localInfo + townInfo);
});
});
</
script
>