markzzz
markzzz

Reputation: 47945

How can I set a value from an asynch DataSource Change Event?

I've this piece of code:

<script type="text/javascript">
    function onCompanyChange(e) {
        // here I need to manage ${Code}
    }
</script>

@(Html.Kendo().DropDownListFor(model => model.Company)
    .Name("Company")
    .DataTextField("Name")
    .DataValueField("Name")
    .DataSource(source =>
    {
        source.Read(read =>
        {
            read.Action("GetCompanies", "Companies");
        })
        .ServerFiltering(false);
    })
    .Events(e => e.Change("onCompanyChange"))
    .Template("<table><tr><td width='300px'>${Name}</td><td width='100px'>${Code}</td><td width='200px'>${State}</td></tr></table>")
    .OptionLabel(Resources.Resources.LblNotDefinedList)
    .Filter("contains")
)

I'd like to send to onCompanyChange the value of ${Code}, which is retrieved (as json) after an asynch request to GetCompanies.

Once I've it, I'll set its value to a MVC HiddenField, and send back to the server.

How can I pass that value through the Kendo change event?

Upvotes: 1

Views: 62

Answers (1)

sandro
sandro

Reputation: 217

You should handle "select" event to get selected item. Something like that from kendo documentation:

function onSelect(e) {
                if ("kendoConsole" in window) {
                    if (e.item) {
                        var dataItem = this.dataItem(e.item);
                        kendoConsole.log("event :: select (" + dataItem.text + " : " + dataItem.value + ")");
                    } else {
                        kendoConsole.log("event :: select");
                    }
                }
            };

But you can get some behavior only with "change" event.

<script type="text/javascript">
    function onCompanyChange(e) {
        // here I need to manage ${Code}
        var selectedIndex = this.select();
        if (selectedIndex != null){
           var selected = this.dataItem(selectedIndex);
           var code = selected.Code;
        }
    }
</script>

Upvotes: 1

Related Questions