Kendo Grid不显示带有Index操作的数据

时间:2015-12-08 04:32:06

标签: asp.net-mvc asp.net-mvc-5 kendo-grid telerik-mvc

我正在使用Kendo Grid for MVC。

以下是我的控制者和行动。

public class ComplainController : Controller
{
    private MSMContext db = new MSMContext();

    public ActionResult Index([DataSourceRequest]DataSourceRequest request)
    {
        var cOMPLAINs = db.COMPLAINs.Include(c => c.MASTER_FAULT);
        var model =  cOMPLAINs.Select(o => new
        {
            JOBSHEET_NO = o.JOBSHEET_NO,
            CUSTOMER_NAME = o.CUSTOMER_NAME,
            CUSTOMER_MOBILE = o.CUSTOMER_MOBILE,
            COMPANY_NAME = o.COMPANY_NAME,
            MODEL_NAME = o.MODEL_NAME
        });
        return Json(model.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
    }
    public ActionResult GetData([DataSourceRequest]DataSourceRequest request)
    {
        var model = db.COMPLAINs.Select(o => new
        {
            JOBSHEET_NO = o.JOBSHEET_NO,
            CUSTOMER_NAME = o.CUSTOMER_NAME,
            CUSTOMER_MOBILE = o.CUSTOMER_MOBILE,
            COMPANY_NAME = o.COMPANY_NAME,
            MODEL_NAME = o.MODEL_NAME
        });
        return Json(model.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
    }
}

现在以下是我在视图中使用的Kendo网格代码。

@(Html.Kendo().Grid<WebMSM.Models.COMPLAIN>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.JOBSHEET_NO).Width(150);
        columns.Bound(p => p.CUSTOMER_NAME).Width(400);
        columns.Bound(p => p.CUSTOMER_MOBILE).Width(200);
        columns.Bound(p => p.COMPANY_NAME).Width(200);
        columns.Bound(p => p.MODEL_NAME).Width(150);
    })
    .Pageable()
    .Sortable()
    .Scrollable()
    .Filterable()
    .HtmlAttributes(new { style = "height:430px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Read(read => read.Action("GetData", "Complain"))
     )
    )

如果我使用GetData操作,Kendo Grid可以正常显示所有数据。 但是如果我使用Index动作,Kendo Grid会显示但没有数据。 使用Index操作时,会显示以下json数据,而不显示任何页面布局和HTML

{"Data":[{"JOBSHEET_NO":1018,"CUSTOMER_NAME":"HEMAL RATHOD","CUSTOMER_MOBILE":"9825369987","COMPANY_NAME":"SAMSUNG","MODEL_NAME":"NOTE 3"},{"JOBSHEET_NO":1019,"CUSTOMER_NAME":"MUKESH CHAUHAN","CUSTOMER_MOBILE":"9825305305","COMPANY_NAME":"APPLE","MODEL_NAME":"IPHONE 6"}],"Total":2,"AggregateResults":null,"Errors":null}

我错过了什么?

1 个答案:

答案 0 :(得分:0)

.DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(20)
    .Read(read => read.Action("GetData", "Complain"))
 )
)

需要更改为

.DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(20)
    .Read(read => read.Action("Index", "Complain"))
 )
)

如果你想让它与“索引”一起使用。如果您的数据源从不调用“索引”操作方法,那么您的网格将始终为空。