Binding Form data to selected item in DropDownList

1 Answer 487 Views
DropDownList
Brian
Top achievements
Rank 1
Brian asked on 24 Oct 2022, 06:14 PM

I have a very simple scenario and can't quite figure out how to manage it with Core and Telerik controls.

I have a DropDownList which I'm successfully filling from an action method. I have form fields below it that relate to the row selected in the DropDownList. What I would like to do is have the form fields change when the selected item in the dropdownlist changes. Simple parent-child relationship, but I can't figure out how to manage this with Core and Telerik. Any suggestions? Thanks.

1 Answer, 1 is accepted

Sort by
0
Aleksandar
Telerik team
answered on 27 Oct 2022, 08:19 AM

Hello Brian,

You could add a Change event handler to the DropDownList. Within that event handler get the selected item via the dataItem client-side method and set the values of the desired components/inputs.

<div class="k-d-flex k-justify-content-center" style="padding-top: 54px;">
    <div class="k-w-300">
        <label for="products">Products:</label>
        @(Html.Kendo().DropDownList()
              .Name("products")
              .DataTextField("ProductName")
              .DataValueField("ProductID")
              .DataSource(source =>
              {
                  source.Read(read =>
                  {
                      read.Action("RemoteDataSource_GetProducts", "DropDownList");
                  });
              })
              .Events(ev=>ev.Change("onChange"))
              .HtmlAttributes(new { style = "width: 100%" })
    )
        <label>Units in Stock:</label>
        @(Html.Kendo().NumericTextBox()
                    .Name("stock")
                    .Format("n0")
                )

        <label>Units on Order:</label>
        @(Html.Kendo().NumericTextBox()
                    .Name("ordered")
                    .Format("n0")
                )
    </div>
</div>
<script>
    function onChange(e){
        var dataItem = e.sender.dataItem();
        console.log(dataItem);
        $("#stock").getKendoNumericTextBox().value(dataItem.UnitsInStock);
        $("#ordered").getKendoNumericTextBox().value(dataItem.UnitsOnOrder);
    }
</script>

Here is a sample REPL.

I hope this helps.

Regards,
Aleksandar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
DropDownList
Asked by
Brian
Top achievements
Rank 1
Answers by
Aleksandar
Telerik team
Share this question
or