kendo ui dropdownlist set selected index

kendo ui dropdownlist set selected index

Hello everyone, I'm going to share the code sample for set the kendo ui dropdownlist using index.

Table of contents

JavaScript code sample

<script type="text/javascript">
   $(function () {
          var cancellationTypes = [
                     { 'ID''FXD''CancellationType''Fixed' },                                                                          { 'ID''Z''CancellationType''Zero' },                                                                              { 'ID''MRB''CancellationType''MRB' }, 
                     { ID: 'MAF''CancellationType''MAF' }];
          
       try {
           $.getJSON("~/api/Company/GetCompany/@session.CancelID",

               function (data) {
                   var index;

                   $.each(cancellationTypes, function (key, val) {
                       if (val.ID == "@Model.CancellationTypeID") {
                           index = key;
                       }
                   });

                   $('#CancellationTypeID').kendoDropDownList({
                       dataTextField: 'CancellationType',
                       dataValueField: 'ID',
                       dataSource: cancellationTypes,
                       index: index                       
                   });
               });
       }
       catch (ex) {
           //Log the exception and alert to the end user
           kendoDialogForEndUserAlert("Error", ex, "Error", ["OK"], null);
       }
   });
</script>

















MVC 4 and Kendo ui DropDownList

@model PCX.Models.Contract
@{
   Layout = "~/Views/Shared/_Layout.cshtml";
   Models.UserSession session = (Models.UserSession)Session["userSession"];
}

@using (Html.BeginForm())
{
   @Html.AntiForgeryToken()
    <div class="form-group">
        @Html.LabelFor(model => model.CancellationType, new { @class = "control-label col-sm-4" })
        <div class="col-sm-8 col-md-6">
            @(Html.Kendo().DropDownListFor(model => model.CancellationType)
            .Name("CancellationTypeID")
            )
            @Html.ValidationMessage("Please select cancellation type")
        </div>
    </div>
}

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.