Hello I am using Kendo for ASP.NET MVC.
I have list of string containing data
[0]="str1"
[1]="str2"... and so on
Now I want to bind this list of string into kendo dropdownlist.
I have bind dropdownlist by list of class with name and id but with only one data in list of string, I don't know how to bind that!
I have done that like below:
@(
Html.Kendo().DropDownList()
.Name("ddlstrings")
.DataTextField("stringname")
.DataValueField("stringname")
//.Events(x => x.Select("sourceclick"))
.SelectedIndex(0)
.DataSource(source =>
{
source.Read(read =>
{
read.Action("getData", "String");
});
})
)
But I got undefined.
I am returning the data like this:
public JsonResult getData()
{
try
{
List<string> stringlist = object.getstrlist();
return Json(stringlist, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json("", JsonRequestBehavior.AllowGet);
}
}
Does anyone have any idea how can I do this!
Any help would be appreciate.
The answer you have provided is right actually. The Action must return List<SelectListItem>
as the output. See this Example and in the code see the BindTo
property.
You can just update your code to below.
public JsonResult getData()
{
try
{
var stringlist = object.getstrlist().select( x=> new SelectListItem
{
Value = x,
Text = x
}).ToList();
return Json(stringlist, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json("", JsonRequestBehavior.AllowGet);
}
}
I have just modified your code to not have a for loop.