Mojtaba Mojtaba - 1 month ago 13
jQuery Question

recieve data in conroller but not showing in kendo grid

I get the data in controller and return it by

json
but in kendo grid i can not showing that data and the grid jost display table and rows of data did send.

my controller code:

private IRepository<Customer> _UserDetail;
public UserDetailsController(IRepository<Customer> UserDetail)
{
_UserDetail = UserDetail;
}

public ActionResult Manage()
{
return View();
}

[HttpPost]
public ActionResult GetUsers(DataSourceRequest userDetail)
{
var query = _UserDetail.Table.Select(x => x.Email).ToList();

var gridModel = new DataSourceResult
{
Data = query,
Total = query.Count
};
return Json(gridModel);
}


my kendo grid view:



<script>
$(document).ready(function() {
$("#user-details").kendoGrid({
dataSource: {
type: "json",
transport: {
read: {
url: "@Html.Raw(Url.Action("GetUsers", "UserDetails"))",
type: "POST",
dataType: "json",

},
},
schema: {
data: "Data",
total: "Total",
errors: "Errors",
},
requestEnd: function(e) {
if (e.type == "update") {
this.read();
}
},
error: function(e) {
display_kendoui_grid_error(e);
// Cancel the changes
this.cancelChanges();
},
serverPaging: true,
serverFiltering: true,
serverSorting: true
},
pageable: {
refresh: true,
numeric: false,
previousNext: false,
info:false
},
editable: {
confirmation: true,
mode: "inline"
},
scrollable: false,
columns: [
{
field: "Email",
title: "User Name",
width: 200
},
{
command: [
{
name: "edit",
text: "@T("Admin.Common.Edit")"
}, {
name: "destroy",
text: "@T("Admin.Common.Delete")"
}
],
width: 200
}
]
});
});
</script>

<div id="user-details"></div>




Answer

i figure that where is my fault.
first of all i have used ICustomerService,actually this way is well form than IRepository.

[HttpPost]
    public JsonResult GetUsers([DataSourceRequest]DataSourceRequest command)
    {
        var details = _CustomerDetail
            .GetAllCustomers()
            .Where(z=>z.Email != null)
            .Select(x => new { x.Email , x.Id })
            .ToList()
            .AsQueryable();

        DataSourceResult result = details.ToDataSourceResult(command);
        return Json(result, JsonRequestBehavior.AllowGet);

    }