KendoUI网格问题

时间:2014-12-27 07:07:56

标签: json kendo-ui jsonp kendo-grid kendo-asp.net-mvc

我有这个KendoUI Grid,它调用一个控制器方法来显示一些数据。调用成功,通过查看Fiddler,我发现实际上返回了数据。但它根本没有显示在列中。

这是我的网格:

$("#someGrid").kendoGrid({
    height: 400,
    columns: [
        { field: "ProductID", width: "60px", title: "ID" },
        { field: "ProductName", width: "150px", title: "Name" },
        { field: "Price", width: "300px", title: "Price" },
        { field: "VAT", width: "100px", title: "VAT" },
        { field: "Discount", width: "100px", title: "Discount" }
    ],
    editable: false, // enable editing
    //editable: "inline",
    //pageable: false,
    //sortable: false,
    //filterable: false,
    dataSource: {
    //    serverPaging: true,
    //    serverFiltering: true,
    //    serverSorting: true,
        pageSize: 10,
        schema: {

            model: { // define the model of the data source. Required for validation and property types.
                id: "ProductID",
                fields: {
                    ProductID: { editable: false, nullable: true},
                    ProductName: { type: "string", editable: false },
                    Price: { type: "number", validation: { required: true, min: 1 } },
                    VAT: { type: "number",  validation: { required: true, min: 1 } },
                    Discount: { type: "number", editable: true, validation: {required: false} }
                }
            }
        },
        //requestEnd: onRequestEnd,
        //batch: true, // enable batch editing - changes will be saved when the user clicks the "Save changes" button
        transport: {
            read: {
                url: "Product/GetPrices?productIDs="+productIDs, //specify the URL which should return the records. This is the Read method of the HomeController.
                contentType: "application/json",
                type: "POST" //use HTTP POST request as by default GET is not allowed by ASP.NET MVC
            },
            parameterMap: (ProductsPriceList)=> JSON.stringify(ProductsPriceList)
            }
        }
    });

这是我的控制器方法:

public ActionResult GetPrices(List<string> productIDs)
{
    var prodIDs = ParseHelper.ParseDelimitedString(productIDs, ',');

    var productsPriceList = new List<ProductPriceVM>();

    var productSelectorVM = new ProductSelectorVM();

    foreach (var prodID in prodIDs)
    {
        if (!string.IsNullOrEmpty(prodID))
        {
            var productPriceVM = new ProductPriceVM();
            int intProdID;
            var result = int.TryParse(prodID, out intProdID);
            var product = _productRepository.GetAllProducts().SingleOrDefault(p => p.ProductID == intProdID);
            if (product != null)
            {
                productPriceVM.ProductName = product.ProductName;
                productPriceVM.ProductID = product.ProductID;

                productPriceVM.Price = product.UnitPrice;

                productPriceVM.VAT = product.VAT;
            }
            productsPriceList.Add(productPriceVM);
            productSelectorVM.ProductPriceList = productsPriceList;
        }
    } 
    return Json(new { ProductsPriceList = productsPriceList.ToList(), ok = true }, JsonRequestBehavior.AllowGet);
}

2 个答案:

答案 0 :(得分:0)

尝试将transport-&gt; read-&gt; dataType更改为json。因为kendo ui datasource是default read,dataType是jsonp。

 transport: {
        read: {
            url: "Product/GetPrices?productIDs="+productIDs, 
            contentType: "application/json",
            type: "POST",
            dataType: "json"
        },
        parameterMap: (ProductsPriceList)=> JSON.stringify(ProductsPriceList)
        }
    }

答案 1 :(得分:0)

试试这个,在parameterMap中,不要将结果字符串化。您已经返回JSON,因此无需对已经是JSON字符串的结果进行字符串化。

仅供参考,HTML5 kendo演示包括:

 if (operation !== "read" && options.models) {
     return {models: kendo.stringify(options.models)};
 }

......换句话说,他们不会将阅读结果字符串化。