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

Dynamic cascdefrom in editor template

3 Answers 165 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Kjell
Top achievements
Rank 1
Kjell asked on 02 Dec 2016, 07:56 PM

Hello, I'm using a cascading combobox in an editor template.  The Id is set dynamically to avoid conflict with multiple instances of the editor on the same page.  So for example I might setup the editor like this:

 

@Html.EditorFor(o => o.MatterNum, "MatterComboBox", new { ControlId = "upload_Matter" })

 

It sets the Id dynamically and that works great ( see below ). The problem I'm having is with cascading, because the parent combobox is also dynamic, so I don't know what the ID will be in my additional data method.  I need to be able to somehow pass the ID of the parent combobox when setting up the child.  Here is how my child combo is defined (the parent is very similar):

 

@(Html.Kendo().ComboBoxFor(o => o)
          .DataTextField("MatterNumName")
          .DataValueField("MatterNum")
          .Placeholder("Select Matter")
          .HtmlAttributes(new { id = @ViewData["ControlId"], style = "width: 100%" })  
          .Filter("contains")
          .AutoBind(true)
          .MinLength(1)
          .DataSource(source =>
          {
              source.Read(read =>
              {
                  read.Action("GetFilteredMatters", "ClientMatter")
                      .Data("onAdditionalMatterData");
              })
                  .ServerFiltering(true);
          })
)

 

I've come at this from a few angles but coming up short each time. 

1. I can set the child/parent IDs to be similar so I could just infer the parent Id from the child.  However I haven't found a way to get the child combobox Id inside the onAdditionalMatterData js function, it seems to only contain the filter data (which I also need). If there was a way to get the sending control information out of that fitler object I think it would solve my issue.

2. I thought maybe I could pass the Id along from the template view to the onAdditionalMatterData function, something like this:

 .Data("eBillReady.ClientMatterLookups.onAdditionalMatterData('parentId')");

but I'm not sure how to do that without losing the filter data which is then no longer passed, also I would obviously need the name to populate dynamically perhaps from the viewdata.

3. I think if I dynamically set "CascadeFrom", then that may cause the Id to be part of that filters object passed to onAdditionalMatterData but I didn't get anywhere with that.  Maybe setting that manually in the page load would work?

 

 

3 Answers, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 06 Dec 2016, 01:16 PM
Hello Kjell,

You can pass additional data to controller. In order to do so the data function needs to return an object with different properties that will hold the data and the controller needs to have parameters named after those properties. Using the third DropDownList from our Cascading DropDownList demo as an example,
instead of passing the handler like this: .Data("filterOrders"); we can include an additional parentId property (the value is hardcoded to 'products') in the object returned by the function:
read.Action("GetCascadeOrders", "ComboBox")
    .Data("function () {return { products: $('#products').val(), parentId: 'products'}}");

And access it in the controller:
public JsonResult GetCascadeOrders(int? products, string parentId)

Regards,
Ivan Danchev
Telerik by Progress
Telerik UI for ASP.NET MVC is ready for Visual Studio 2017 RC! Learn more.
0
Kjell
Top achievements
Rank 1
answered on 06 Dec 2016, 04:44 PM

Thank you for responding but I already have it working as a hardcoded value, I need it to be dynamic. 

In fact I don't need the parent Id in the controller, I'm trying to pass the parent Id to the java function which is called by ".Data".  In your example the parentId could actually be left off, however I need "#products" to be dynamic not hardcoded.

The reason I want it to be dynamic is so that I can use an editor template and just pass that a couple parameters rather than defining the combobox separately everywhere I use it.

0
Ivan Danchev
Telerik team
answered on 07 Dec 2016, 03:43 PM
Hello Kjell,

We haven't been able to find a way to pass the id as a parameter to the function. You could consider saving the id in a hiddenfield and accessing its value in the function's handler with jQuery: $("#myHiddenField1").val()

Regards,
Ivan Danchev
Telerik by Progress
Telerik UI for ASP.NET MVC is ready for Visual Studio 2017 RC! Learn more.
Tags
ComboBox
Asked by
Kjell
Top achievements
Rank 1
Answers by
Ivan Danchev
Telerik team
Kjell
Top achievements
Rank 1
Share this question
or