asp.net核心中的DataSourceResult

时间:2017-07-12 20:35:16

标签: asp.net-mvc kendo-ui asp.net-core asp.net-core-webapi

当我在Asp.net Mvc工作时,对于分页数据使用KendoUi。此代码用于Asp.net Mvc Web api

 public DataSourceResult Get(HttpRequestMessage requestMessage)
        {
            var request = JsonConvert.DeserializeObject<DataSourceRequest>(
                requestMessage.RequestUri.ParseQueryString().GetKey(0)
            );
            WebApplicationDbContext db = new WebApplicationDbContext();
            var list = db.Product.ToList();
            return list.AsQueryable()
                       .ToDataSourceResult(request.Take, request.Skip, request.Sort, request.Filter);
        }

现在,当我使用此代码时使用Asp.net Core,它不起作用。 在错误列表中向我显示此错误

  

&#39;乌里&#39;不包含&#39; ParseQueryString&#39;的定义和不   扩展方法&#39; ParseQueryString&#39;接受第一个类型的参数   &#39;乌里&#39;可以找到(你错过了使用指令或程序集   引用?)

如何在Asp.net Core中使用此代码?

3 个答案:

答案 0 :(得分:1)

首先删除HttpRequestMessage requestMessage参数。然后删除requestMessage.RequestUri.ParseQueryString().GetKey(0)部分并将其替换为:

var rawQueryString = this.HttpContext.Request.QueryString.ToString();
// PM> Install-Package Microsoft.AspNetCore.WebUtilities
var rawQueryStringKeyValue = QueryHelpers.ParseQuery(rawQueryString).FirstOrDefault();
var dataString = Uri.UnescapeDataString(rawQueryStringKeyValue.Key); // this is your received JSON data from Kendo UI

答案 1 :(得分:0)

我不确定为什么需要反序列化请求。我通常会将request传递给ToDataSourceResult扩展方法。

For example,

public JsonResult Get([DataSourceRequest] DataSourceRequest request)
{
   var db = new WebApplicationDbContext();
   return db.Product.ToDataSourceResult(request);
}

答案 2 :(得分:0)

谢谢VahidN
这个项目给了我很多帮助 KendoUI.Core.Samples

我在Controller

中使用此代码
 public DataSourceResult GetProducts()
        {
            var dataString = this.HttpContext.GetJsonDataFromQueryString();
            var request = JsonConvert.DeserializeObject<DataSourceRequest>(dataString);

            var list = ProductDataSource.LatestProducts;
            return list.AsQueryable()
                       .ToDataSourceResult(request.Take, request.Skip, request.Sort, request.Filter);
        }


并在chstml

中使用此代码
@{
    ViewData["Title"] = "Home Page";
}

<!--Right to left grid-->
<div class="k-rtl">
    <div id="report-grid"></div>
</div>

@section Scripts
{
    <script type="text/javascript">
        $(function () {
            var productsDataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "@Url.Action("GetProducts", "Sample03")",
                        dataType: "json",
                        contentType: 'application/json; charset=utf-8',
                        type: 'GET'
                    },
                    parameterMap: function (options) {
                        return kendo.stringify(options);
                    }
                },
                schema: {
                    data: "data",
                    total: "total",
                    model: {
                        fields: {
                            "id": { type: "number" }, //Determine the field for dynamic search
                            "name": { type: "string" },
                            "isAvailable": { type: "boolean" },
                            "price": { type: "number" }
                        }
                    }
                },
                error: function (e) {
                    alert(e.errorThrown);
                },
                pageSize: 10,
                sort: { field: "id", dir: "desc" },
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true
            });

            $("#report-grid").kendoGrid({
                dataSource: productsDataSource,
                autoBind: true,
                scrollable: false,
                pageable: true,
                sortable: true,
                filterable: true,
                reorderable: true,
                columnMenu: true,
                columns: [
                    { field: "id", title: "RowNumber", width: "130px" },
                    { field: "name", title: "ProductName" },
                    {
                        field: "isAvailable", title: "Available",
                        template: '<input type="checkbox" #= isAvailable ? checked="checked" : "" # disabled="disabled" ></input>'
                    },
                    { field: "price", title: "Price", format: "{0:c}" }
                ]
            });
        });
    </script>
}