Setting selected value from session

3 posts, 1 answers
  1. Mark
    Mark avatar
    90 posts
    Member since:
    Jul 2014

    Posted 06 Jul 2015 Link to this post

    Hi there,

    I have a page with multiple Kendo dropdownlists on it, working fine.  However, I want each to have the selected value set based on a session value.  I have approached this in 2 ways :-

    a) Return a JSON-ed list of items, with the appropriate one marked as selected, and then bind the .Value property to the selected value
    b) Use JS after the list is ready to set the selected item

    I can't get either to work.  My code is below, please assist.

    Thanks, Mark

    MVC VIEW CSHTML :-

     @( Html.Kendo().DropDownList()
                .Name("compliances")
                .DataTextField("Text")
                .DataValueField("Value")
                .Filter("contains")
                .Value("Selected")
                .DataSource(source =>
                {
                    source.Read(read =>
                    {
                        read.Action("PopulateComplianceFilter", "Site");
                    })
                    .ServerFiltering(false);
                })
                .Events(e =>
                {
                    e.Change("ComplianceChange");
                })
            )

    CONTROLLER C# CODE :-

        // Populate compliances on filter panel
                public ActionResult PopulateComplianceFilter([DataSourceRequest]DataSourceRequest request)
                {
                    List<Compliance> col = Enum.GetValues(typeof(Compliance)).Cast<Compliance>().ToList();
                    Array values = Enum.GetValues(typeof(Compliance));
                    List<ListItem> items = new List<ListItem>(values.Length);

                    // convert enum list to select item list to get values displayed on front end
                    foreach (Enum i in values)
                    {
                        items.Add(new ListItem
                        {
                            Text = EnumHelper.GetDisplayName(i),
                            Value = ((int)(Compliance)i).ToString(),
                            Selected = (((int)(Compliance)i) == sessionHelper.SiteFilter_ComplianceID)?true:false
                        });
                    }
                    return Json(items, JsonRequestBehavior.AllowGet);
                }

  2. Answer
    Georgi Krustev
    Admin
    Georgi Krustev avatar
    3707 posts

    Posted 08 Jul 2015 Link to this post

    Hello Mark,

    In order to select the DropDownList value then use its Value("value") method:
    @( Html.Kendo().DropDownList()
        .Name("compliances")
        ...
        .Value(sessionHelper.SiteFilter_ComplianceID.toString()); //you need to retrieve access to the sessionHelper in the View
    )

    Setting the "Selected" property will not work in this case, because remote binding is used. The best way to accomplish your goal is just set the selected value as it is shown in the above example.

    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
  4. Mark
    Mark avatar
    90 posts
    Member since:
    Jul 2014

    Posted 08 Jul 2015 in reply to Georgi Krustev Link to this post

    Worked perfect, thanks :)
Back to Top