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

Setting selected value from session

2 Answers 182 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 06 Jul 2015, 09:01 AM

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 Answers, 1 is accepted

Sort by
0
Accepted
Georgi Krustev
Telerik team
answered on 08 Jul 2015, 07:29 AM
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!
 
0
Mark
Top achievements
Rank 1
answered on 08 Jul 2015, 08:50 AM
Worked perfect, thanks :)
Tags
DropDownList
Asked by
Mark
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Mark
Top achievements
Rank 1
Share this question
or