Undefined in DropDownList

2 posts, 0 answers
  1. SKNBI
    SKNBI avatar
    1 posts
    Member since:
    May 2016

    Posted 19 May Link to this post

    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

  2. Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 23 May Link to this post

    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!
     
  3. Kendo UI is VS 2017 Ready
Back to Top