Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
public JsonResult GetSates()
       {
           try
           {
               var statelist = (from states in detailsContext.Statess select states.StateName);// here i'm getting data from my db
               return Json(new { statelist }, JsonRequestBehavior.AllowGet);
           }
           catch (Exception)
           {
               throw new ApplicationException("Error");
           }
       }




Thanks in Advanced...
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900