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

DropDownLists Shared Datasource binding to subobject of model

1 Answer 175 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Cynthia
Top achievements
Rank 1
Cynthia asked on 28 Oct 2019, 04:04 PM

Hello,
I have a model with a few lists.  I am trying to use a shared datasource to populate a dropdownlist for each of these lists but I am having trouble with how to refer to a specific subobject within the object that is returned from the controller.  I’m sure that this has been done before… I’m just having trouble finding an example.

My model looks like this:
public class BillModel : DocumentBaseModel
    {
        …

        public List<HistoryModel> History { get; set; }
        public List<AmendmentModel> Amendments { get; set; }
        public List<OtherDocModel> OtherDocs { get; set; }
        public List<PreviousVersionModel> PreviousVersions { get; set; }
    }

My controller returns the BillModel
My view has a grid that accesses a ClientTemplate that has three ddls and my shared datasource.
@(Html.Kendo().DataSource<BillModel>()
.Name("myDataSource")
       .Custom(dataSource =>
       {
              dataSource
                     .Type("json")
                     .Transport(transport =>
                     {
                           transport.Read(read =>
                           {
                              read.Action("GetBill", "Bills", new { billNo = "#=Id#"});
                            });
                     });
       })
)
@(Html.Kendo().DropDownList()
.Name("amendments")
       .DataTextField("Name")
       .DataValueField("LFID")
       .DataSource("myDataSource")
       .HtmlAttributes(new { style = "width: 100%" })
  .ToClientTemplate()
)

@(Html.Kendo().DropDownList()
.Name("others")
       .DataTextField("Name")
       .DataValueField("LFID")
       .DataSource("myDataSource")
       .HtmlAttributes(new { style = "width: 100%" })
  .ToClientTemplate()
)

@(Html.Kendo().DropDownList()
.Name("previous")
       .DataTextField("Name")
       .DataValueField("LFID")
       .DataSource("myDataSource")
       .HtmlAttributes(new { style = "width: 100%" })
  .ToClientTemplate()
)

How do I get each ddl to use their list from the datasource?  I thought perhaps I could use dot notation and write .DataTextField(“Amendments.Name”).  It didn’t yell at me it just said undefined.  I’m sure it must be something simple I just can’t figure it out today.

1 Answer, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 31 Oct 2019, 02:24 PM

Hi,

The shared dataSource option is designed to return a dataSource collection that could be shared between the components rather then to provide lots of dataSources in one object.

Having this in mind we don't have a direct MVC setting that could make the desired scenario work. A recommended approach in general is to use a separate dataSource for each collection on the page. Yet if you need to have all the data with one request  one possible workaround is to use javascript and  reset the dataSource by using the setDataSource function of the DropDownList components as for example it is done in the code below:

<script>
    function requestEnd(e) {
        $("#amendments").data("kendoDropDownList").setDataSource(e.response.products);
    }
</script>

 .Custom(dataSource =>
                   {
                       dataSource
                      .Type("json")
                      .Events(e=>e.RequestEnd("requestEnd"))

Regards,
Plamen
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
DropDownList
Asked by
Cynthia
Top achievements
Rank 1
Answers by
Plamen
Telerik team
Share this question
or