Setting default value in kendodropdown from JSON data

2 Answers 5061 Views
DropDownList
Lloyd
Top achievements
Rank 1
Lloyd asked on 16 Sep 2015, 11:34 AM

Please find the below code as i want to set default value of dropdown instead of optionlable from  JSON data which is returned
from controller .
 
Please find the below code

public ActionResult GetOrders()
       {
           List<usp_IDQ_GetLicenseDistrictsVO> objresult1 = new List<usp_IDQ_GetLicenseDistrictsVO>();
           objresult1 = objConnection.usp_IDQ_GetLicenseDistricts(User.Identity.Name, true).ToList();
           return Json(objresult1, JsonRequestBehavior.AllowGet); ;
       }

@(Html.Kendo().DropDownList()
              .Name("DropDownList1")

                      //.Events(ev => ev.DataBound("SalesPersonID_DataBound"))
                      .OptionLabel("Select ...")
              .HtmlAttributes(new { style = "width:300px" })
              .OptionLabel("Select ...")
              .DataTextField("District_Id")
              .DataValueField("Location_ID")


                      .DataSource(source =>
           {
               source.Read(read =>
                  {
                      read.Action("GetOrders", "IDTDashboard");
                  })
                  .ServerFiltering(true);
           })
           .AutoBind(false)


2 Answers, 1 is accepted

Sort by
0
Kiril Nikolov
Telerik team
answered on 18 Sep 2015, 08:19 AM

Hello Lloyd,

 

If you want to set a default value of the DropDownList you can use the value configuration option. It should be something like this:

 

@(Html.Kendo().DropDownList()
          .Name("products")
          .DataTextField("ProductName")
          .DataValueField("ProductID")
          .Value("7")

....

It is documented here as well:

 

http://docs.telerik.com/kendo-ui/api/javascript/ui/dropdownlist#configuration-value

 

Regards,

Kiril Nikolov

Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Lloyd
Top achievements
Rank 1
commented on 18 Sep 2015, 10:48 AM

How we will set the Json value in .Value feild? As Value("7") is harcoded feild.
Kiril Nikolov
Telerik team
commented on 22 Sep 2015, 06:05 AM

Hello Lloyd,

 

You can use the JavaScript API to set the value of the widget, at any point after the initialization. Please check the following article and let me know if it helps:

 

http://docs.telerik.com/kendo-ui/api/javascript/ui/dropdownlist#methods-value

 

Regards,
Kiril Nikolov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Lloyd
Top achievements
Rank 1
commented on 22 Sep 2015, 10:39 AM

even using this post refernce, we have to set harcode value using javascript.

But i want to set default value of kendo dropdown  using my runtime JSON result.

0
Kiril Nikolov
Telerik team
answered on 22 Sep 2015, 11:00 AM

Hello Lloyd,

 

I think that I understand the issue more clearly now - you want to use the JSON response from the server to set the value of the widget, based on a property there. If this is the case, then you can use the dataBound event, and set the value of the widget there using the value() method suggested in the previous post. Please see the documentation about the dataBound event here:

 

http://docs.telerik.com/kendo-ui/api/javascript/ui/dropdownlist#events-dataBound

 

Regards,
Kiril Nikolov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
DropDownList
Asked by
Lloyd
Top achievements
Rank 1
Answers by
Kiril Nikolov
Telerik team
Share this question
or