We're upgrading from Telerik MVC to Kendo, and have followed the migration instructions and looked at docs and forum posts for two cascading dropdown lists. However, the revised code is not working - the cascading controller event never fires (it's in the same controller as the view). One caveat is that the dropdowns are inside a grid detail, so the controller method had to peek into Request.Form[0] to get the argument. But, that controller method isn't even firing in the revised code. UI-wise, it looks as if the cascading code is trying to refresh the dropdown, but no data ever shows up.
Old code:
@(Html.Telerik().DropDownListFor(i => item.CountryCodeID)
.Name("addressEditCountryCodeID_" + item.ListIndex)
.BindTo(new SelectList(countries, "key", "value", item.CountryCodeID))
.CascadeTo("addressEditState_" + item.ListIndex)
.HtmlAttributes(new { @class = "dropDownList", style = "width: 250px; float:left; margin-right: 10px;" })
)
...
@(Html.Telerik().DropDownListFor(model => item.State)
.Name("addressEditState_" + item.ListIndex)
.DataBinding(binding => binding.Ajax().Select("GetStatesByCountryCode", "Recipients"))
.ClientEvents(c => c.OnDataBound("getStateValue"))
.HtmlAttributes(new { @class = "dropDownList", style = "width: 90px; float:left; margin-right: 10px;" })
)
New code:
@(Html.Kendo().DropDownListFor(i => item.CountryCodeID)
.Name("addressEditCountryCodeID_" + item.ListIndex)
.BindTo(new SelectList(countries, "key", "value", item.CountryCodeID))
.DataTextField("Text")
.DataValueField("Value")
.HtmlAttributes(new { @class = "dropDownList", style = "width: 250px; float:left; margin-right: 10px;" })
)
...
@(Html.Kendo().DropDownListFor(model => item.State)
.Name("addressEditState_" + item.ListIndex)
.CascadeFrom("addressEditCountryCodeID_" + item.ListIndex)
// .DataBinding(binding => binding.Ajax().Select("GetStatesByCountryCode", "Recipients"))
.DataSource(source => {
source.Read(read =>
{
read.Action("GetStatesByCountryCode", "Recipients");
})
.ServerFiltering(true);
})
.DataTextField("Text")
.DataValueField("Value")
.Events(c => c.DataBound("getStateValue"))
.HtmlAttributes(new { @class = "dropDownList", style = "width: 90px; float:left; margin-right: 10px;" })
)
Controller method:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GetStatesByCountryCode()
{
// Because we can have multiple addresses and Telerik hard-codes the control name as a parameter,
// we're using Request.Form[0] in order to extract the value.
List<USState> result = new List<USState>();
int countryCodeId;
if (int.TryParse(Request.Form[0].ToString(), out countryCodeId))
{
result = LookupService.GetStateListByCountryCode((int)countryCodeId).ToList();
if (result.Count > 0)
{
result.Insert(0, new USState() { Code = "", Name = "" }); // add a blank at the beginning.
}
}
return new JsonResult { Data = new SelectList(result, "Code", "Code") };
}
Ideas? (Or just another pair of eyes?)
Jim Stanley
Blackboard Connect
Old code:
@(Html.Telerik().DropDownListFor(i => item.CountryCodeID)
.Name("addressEditCountryCodeID_" + item.ListIndex)
.BindTo(new SelectList(countries, "key", "value", item.CountryCodeID))
.CascadeTo("addressEditState_" + item.ListIndex)
.HtmlAttributes(new { @class = "dropDownList", style = "width: 250px; float:left; margin-right: 10px;" })
)
...
@(Html.Telerik().DropDownListFor(model => item.State)
.Name("addressEditState_" + item.ListIndex)
.DataBinding(binding => binding.Ajax().Select("GetStatesByCountryCode", "Recipients"))
.ClientEvents(c => c.OnDataBound("getStateValue"))
.HtmlAttributes(new { @class = "dropDownList", style = "width: 90px; float:left; margin-right: 10px;" })
)
New code:
@(Html.Kendo().DropDownListFor(i => item.CountryCodeID)
.Name("addressEditCountryCodeID_" + item.ListIndex)
.BindTo(new SelectList(countries, "key", "value", item.CountryCodeID))
.DataTextField("Text")
.DataValueField("Value")
.HtmlAttributes(new { @class = "dropDownList", style = "width: 250px; float:left; margin-right: 10px;" })
)
...
@(Html.Kendo().DropDownListFor(model => item.State)
.Name("addressEditState_" + item.ListIndex)
.CascadeFrom("addressEditCountryCodeID_" + item.ListIndex)
// .DataBinding(binding => binding.Ajax().Select("GetStatesByCountryCode", "Recipients"))
.DataSource(source => {
source.Read(read =>
{
read.Action("GetStatesByCountryCode", "Recipients");
})
.ServerFiltering(true);
})
.DataTextField("Text")
.DataValueField("Value")
.Events(c => c.DataBound("getStateValue"))
.HtmlAttributes(new { @class = "dropDownList", style = "width: 90px; float:left; margin-right: 10px;" })
)
Controller method:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GetStatesByCountryCode()
{
// Because we can have multiple addresses and Telerik hard-codes the control name as a parameter,
// we're using Request.Form[0] in order to extract the value.
List<USState> result = new List<USState>();
int countryCodeId;
if (int.TryParse(Request.Form[0].ToString(), out countryCodeId))
{
result = LookupService.GetStateListByCountryCode((int)countryCodeId).ToList();
if (result.Count > 0)
{
result.Insert(0, new USState() { Code = "", Name = "" }); // add a blank at the beginning.
}
}
return new JsonResult { Data = new SelectList(result, "Code", "Code") };
}
Ideas? (Or just another pair of eyes?)
Jim Stanley
Blackboard Connect