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

Undefined in DropDownList

1 Answer 2619 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
SKNBI
Top achievements
Rank 1
SKNBI asked on 19 May 2016, 05:18 PM

I am new to Telerik, I am getting in the options list of the drop down list "years" Undefined instead of 2017, 2015, ...

Any help is appreciated.  Below are the codes.

This is the view:

@(Html.Kendo().DropDownList()
              .Name("years")
              .HtmlAttributes(new { style = "width:100%" })
              .OptionLabel("Select year...")
              .DataTextField("YearName")
              .DataValueField("YearCode")
              .DataSource(source =>
              {
                  source.Read(read =>
                  {
                      read.Action("GetCascadeYears","Home");
                  });
              })
)
 and this is the controller:

 public JsonResult GetCascadeYears()
        {
            IQueryable years = Year.GetYears();

            if (HttpContext.Request.IsAjaxRequest())
            {
                return Json(new SelectList(
                            years,"YearCode", "YearName"), JsonRequestBehavior.AllowGet
                            );
            }

            return View(years);
        }
and this the model:

public class Year
    {

        public int YearCode { get; set; }
        public String YearName { get; set; }

        public static IQueryable<Year> GetYears()
        {
            return new List<Year>
            {
                new Year {
                    YearCode = 1,
                    YearName = "2017"
                },
                new Year{
                    YearCode = 2,
                    YearName = "2015"
                },
                new Year{
                    YearCode = 3,
                    YearName = "2014"
                },
                new Year{
                    YearCode = 4,
                    YearName = "2010"
                },

            }.AsQueryable();
        }


    }

Regards

1 Answer, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 23 May 2016, 01:27 PM
Hello SKNBI,

The widget displays "undefined", because the service returns an array with "Text" and "Value" fields (SelectListItem objects). The dropdownlist on the other hand is configured to expect objects with "YearName" and "YearCode", which is not present in the data. Hence the widget will display "undefined".

I would suggest you return the result of the GetYears() method directly. Thus the widget will be able to find the correct properties and display the year values.

Regards,
Georgi Krustev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
DropDownList
Asked by
SKNBI
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Share this question
or