Dynamic cascdefrom in editor template

4 posts, 0 answers
  1. Kjell
    Kjell avatar
    281 posts
    Member since:
    Jun 2010

    Posted 02 Dec 2016 Link to this post

    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?

     

     

  2. Ivan Danchev
    Admin
    Ivan Danchev avatar
    1039 posts

    Posted 06 Dec 2016 Link to this post

    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.
  3. Kjell
    Kjell avatar
    281 posts
    Member since:
    Jun 2010

    Posted 06 Dec 2016 in reply to Ivan Danchev Link to this post

    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.

  4. Ivan Danchev
    Admin
    Ivan Danchev avatar
    1039 posts

    Posted 07 Dec 2016 Link to this post

    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.
Back to Top