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);
}