This is a migrated thread and some comments may be shown as answers.

[Solved] Cascade Dropdowns?

2 Answers 313 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Cyndie
Top achievements
Rank 1
Cyndie asked on 14 Sep 2010, 08:53 PM
Is there a way or an example of how to update one dropdown based on the selected value of another? 

2 Answers, 1 is accepted

Sort by
0
Adam Gritt
Top achievements
Rank 1
answered on 22 Sep 2010, 08:15 PM
I was looking to do the same thing recently.  I found that several of the Javascript/jQuery scripts online wouldn't work as Telerik doesn't use the standard select element for their drop downs.  However, I did find that it was easy enough to do manually.  I got it to work by using the following code.

ASPX:
<% Html.Telerik().ComboBox()
   .Name("TestDDL")
   .DataBinding(db => db.Ajax().Select("_AjaxListBinding", "Home"))
   .ClientEvents(ce =>
   {
      ce.OnChange("onChange");
   })
   .Render();
%>
<br />
<br />
<% Html.Telerik().ComboBox()
   .Name("TestDDL2")
   .DataBinding(db => db.Ajax().Select("_AjaxList2Binding", "Home"))
   .ClientEvents(ce =>
   {
      ce.OnLoad("onLoad");
   })
   .Render();
 %>

Javascript:
function onChange(e) {
   var ddl = $('#TestDDL2').data('tComboBox');
   if (ddl.ajax) {
      start = '?';
      if (ddl.backupAjax.selectUrl.indexOf('?') != -1) {
         start = '&';
      }
      ddl.ajax.selectUrl = ddl.backupAjax.selectUrl + start + "itemId=" + e.value;
      ddl.reload();
   }
}
function onLoad() {
   var ddl = $('#TestDDL2').data('tComboBox');
   if (ddl && ddl.ajax) {
      ddl.backupAjax = new Object;
      ddl.backupAjax.selectUrl = ddl.ajax.selectUrl;
   }
}

Controller Methods:
public ActionResult _AjaxListBinding()
{
   return new JsonResult { Data = GetListData() };
}
 
public ActionResult _AjaxList2Binding(int? itemId)
{
   return new JsonResult { Data = GetDependantListData(itemId) };
}

I would like to create a jQuery plugin script that would handle this for me (ie attaching events, modifying query, etc) but I haven't had the time/need to do so as of yet.
0
Cyndie
Top achievements
Rank 1
answered on 27 Sep 2010, 02:04 PM
Hi Adam,

Thanks for the response.  I was able to get this working using some of the guidenace from other forum posts, mainly this one:
http://www.telerik.com/community/forums/aspnet-mvc/combobox/load-on-demand-combobox-items.aspx


I've also requested Cascading Dropdowns as a feature request.  It can be voted on it PITS (ID 3550):
http://www.telerik.com/support/pits.aspx#/public/aspnet-mvc/3550

If there is enough interest, hopefully it will get implemented in a future release.
Tags
ComboBox
Asked by
Cyndie
Top achievements
Rank 1
Answers by
Adam Gritt
Top achievements
Rank 1
Cyndie
Top achievements
Rank 1
Share this question
or