Greetings of the day,
Here i want to bind the db data into kendo grid dropdownlist, here i'm sharing my code.
$(document).ready(function () {
var personViewModel = kendo.observable({
dataSource: new kendo.data.DataSource({
transport:
{
read: {
url: "/Details/GetPDetails",
contentType: "application/json;charset=utf-8",
dataType: "json",
type: "GET"
},
parameterMap: function (data, operation) {
if (operation != 'read') {
return JSON.stringify({ personViewModel: data });
}
}
},
schema: {
model:
{
id: "Id",
fields:
{
Id: { type: "number" },
Name: { type: "string", eiditable: true },
Age: { type: "number", editable: true },
StateName: { type: "string", editable: true }
}
},
parse: function (data) {
debugger
if (data.success) {
$("#grid").data("kendoGrid").dataSource.read();
return data.getData;
}
else {
alert(data.message);
return [];
}
}
}
}),
personGridData: function () {
debugger
$('#grid').kendoGrid({
sortable: true,
filterable: true,
toolbar: ["create"],
pageable: {
refresh: true,
pageSizes: true,
buttonCount: 5
},
refresh: true,
autosync: true,
editable: "inline",
columns: [
{ field: "Name", title: "Name", width: 180 },
{ field: "Age", title: "Age", width: 180 },
{ field: "StateName", title: "State Belongs to", width: 200, editor: DropdownState },
{ command: ["edit", "destroy"], title: " ", width: "250px" }
]
});
}
});
personViewModel.personGridData();
personViewModel.dataSource.fetch(function () {
debugger
kendo.bind($('#grid'), personViewModel)
});
function DropdownState(container, options) {
debugger
$('<input id="dropdown-states" required data-text-field="StateName" data-value-field="StateName" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoDropDownList({
dataTextField: 'StateName',
dataValueField: "Id",
dataSource: {
transport: {
read: {
url: '/Details/GetSates',
contentType: "application/json;charset=utf-8",
dataType: "JSON",
type: "GET"
}
},
dataBind: true,
}
});
}
});
</script>
</head>
<body>
</body>
and in controller my code is
public JsonResult GetSates()
{
try
{
var statelist = (from states in detailsContext.Statess select states.StateName);
return Json(new { statelist }, JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
throw new ApplicationException("Error");
}
}
Thanks in Advanced...