Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json jQuery Bootgrid-带有ASP.NET MVC操作的Ajax排序参数_Json_Asp.net Mvc_Asp.net Web Api2_Model Binding_Jquery Bootgrid - Fatal编程技术网

Json jQuery Bootgrid-带有ASP.NET MVC操作的Ajax排序参数

Json jQuery Bootgrid-带有ASP.NET MVC操作的Ajax排序参数,json,asp.net-mvc,asp.net-web-api2,model-binding,jquery-bootgrid,Json,Asp.net Mvc,Asp.net Web Api2,Model Binding,Jquery Bootgrid,我设法创建了一个ApiController,从我的存储库中检索数据,并使用Ajax通过Bootgrid在我的视图中填充一个网格。这是发送到Api操作的请求数据示例,由“查找后正文请求”选项卡给出: current=1&rowCount=10&sort[sender]=asc&searchPhrase=&id=b0df282a-0d67-40e5-8558-c9e93b7befed 以下是一个示例URL: http://localhost/api/SomeCont

我设法创建了一个ApiController,从我的存储库中检索数据,并使用Ajax通过Bootgrid在我的视图中填充一个网格。这是发送到Api操作的请求数据示例,由“查找后正文请求”选项卡给出:

current=1&rowCount=10&sort[sender]=asc&searchPhrase=&id=b0df282a-0d67-40e5-8558-c9e93b7befed
以下是一个示例URL:

http://localhost/api/SomeController?current=1&rowCount=10&sort%5BName%5D=asc&searchPhrase=&id=b0df282a-0d67-40e5-8558-c9e93b7befed
我创建了两个助手类来处理必须作为响应返回的数据,并将数据作为数组进行排序:

public class SortData
{
    public string Field { get; set; } // FIeld Name
    public string Type { get; set; } // ASC or DESC
}

public class BootgridResponseData<T> where T: class
{
    public int current { get; set; } // current page
    public int rowCount { get; set; } // rows per page
    public IEnumerable<T> rows { get; set; } // items
    public int total { get; set; } // total rows for whole query
}
因此,我的行动如下:

    public BootgridResponseData<SomeViewModel> Get(int current, int rowCount, List<SortData> sort, string searchPhrase, string id)
    {
       // get items and return a bootgrid response data with them...
    }
方法被调用,所有参数都正确地与数据一起提供,但sort除外,它始终为null


对此,我应该使用什么样的参数?我还尝试放置对象,但它还是空的。

我也一直在努力解决这个问题。你想得太多了。创建简单的模型来处理来自jQueryBootGrid的post调用很好,但是也可以在post方法中使用简单的参数。至于排序,它看起来像一个键值对,但不能正确序列化

最后我尝试了一个Dictionary对象,它成功了

这是我的签名:

[HttpPost]
public async Task<ActionResult> GetActiveDogs(int? current, int? rowCount, 
                Dictionary<string, string> sort, string searchPhrase = null)

我也为此挣扎过。你想得太多了。创建简单的模型来处理来自jQueryBootGrid的post调用很好,但是也可以在post方法中使用简单的参数。至于排序,它看起来像一个键值对,但不能正确序列化

最后我尝试了一个Dictionary对象,它成功了

这是我的签名:

[HttpPost]
public async Task<ActionResult> GetActiveDogs(int? current, int? rowCount, 
                Dictionary<string, string> sort, string searchPhrase = null)

我在将排序选项传递给我的Web服务时遇到了同样的问题。字典对象也不能解决我的问题。 为了解决这个问题,我创建了一个类,其中包含了我希望通过bootgrid排序选项传递的每个字段的字符串属性。参见代码摘录

class TestSort
{
   public string field1 { get; set; }
   public string field2 { get; set; }
   ...
}
我使用这个类作为Web服务中的排序选项参数。此类中由bootgrid选项引用的所有字段都设置为ASC或DESC。其他字段保持为空


我向该类添加了一个“orderBy”属性,该属性为非空字段返回orderBy子句

我在将排序选项传递给我的Web服务时遇到了同样的问题。字典对象也不能解决我的问题。 为了解决这个问题,我创建了一个类,其中包含了我希望通过bootgrid排序选项传递的每个字段的字符串属性。参见代码摘录

class TestSort
{
   public string field1 { get; set; }
   public string field2 { get; set; }
   ...
}
我使用这个类作为Web服务中的排序选项参数。此类中由bootgrid选项引用的所有字段都设置为ASC或DESC。其他字段保持为空


我向该类添加了一个“orderBy”属性,该属性为非空字段返回orderBy子句

在学习了更多之后,我看到Bootgrid有一个requestHandler,允许您操作发送到服务器的数据

我在javascript中这样做:

var grid = $("#my-grid").bootgrid({
        ajax: true,
        rowCount: 10,
        ajaxSettings: {
            method: "POST",
            cache: true
        },
        requestHandler: function (request) {
            // Scan the original sort object from Bootgrid...
            // and populate an array of "SortData"...
            request.sortItems = [];
            if (request.sort == null)
                return request;
            for (var property in request.sort) {
                if (request.sort.hasOwnProperty(property)) {
                    request.sortItems.push({ Field: property, Type: request.sort[property] });
                }
            }
            return request;
        },
        url: "/api/FooApi"
    });
public class FooApiController : ApiController
{

    [HttpPost]
    public BootgridResponseData<FooModel> Get(BootgridRequestData model)
    {
        // This would come from some data store, using the request params...
        // I use PagedList to make pagination easier...
        IPagedList<FooModel> itemsPaged = store.GetPagedFoo();

        // Then return the response with data...
        return new BootgridResponseData<FooModel>()
        {
            current = model.current,
            rowCount = model.rowCount,
            rows = itemsPaged,
            total = itemsPaged.TotalItemCount
        };
    }
}
然后我在API中创建了我的post操作,如下所示:

var grid = $("#my-grid").bootgrid({
        ajax: true,
        rowCount: 10,
        ajaxSettings: {
            method: "POST",
            cache: true
        },
        requestHandler: function (request) {
            // Scan the original sort object from Bootgrid...
            // and populate an array of "SortData"...
            request.sortItems = [];
            if (request.sort == null)
                return request;
            for (var property in request.sort) {
                if (request.sort.hasOwnProperty(property)) {
                    request.sortItems.push({ Field: property, Type: request.sort[property] });
                }
            }
            return request;
        },
        url: "/api/FooApi"
    });
public class FooApiController : ApiController
{

    [HttpPost]
    public BootgridResponseData<FooModel> Get(BootgridRequestData model)
    {
        // This would come from some data store, using the request params...
        // I use PagedList to make pagination easier...
        IPagedList<FooModel> itemsPaged = store.GetPagedFoo();

        // Then return the response with data...
        return new BootgridResponseData<FooModel>()
        {
            current = model.current,
            rowCount = model.rowCount,
            rows = itemsPaged,
            total = itemsPaged.TotalItemCount
        };
    }
}
然后我甚至可以使用我原来的SortData助手类:

public class SortData
{
    public string Field { get; set; } // FIeld Name
    public string Type { get; set; } // ASC or DESC
}

在学习了更多之后,我看到Bootgrid有一个requestHandler,它允许您操作发送到服务器的数据

我在javascript中这样做:

var grid = $("#my-grid").bootgrid({
        ajax: true,
        rowCount: 10,
        ajaxSettings: {
            method: "POST",
            cache: true
        },
        requestHandler: function (request) {
            // Scan the original sort object from Bootgrid...
            // and populate an array of "SortData"...
            request.sortItems = [];
            if (request.sort == null)
                return request;
            for (var property in request.sort) {
                if (request.sort.hasOwnProperty(property)) {
                    request.sortItems.push({ Field: property, Type: request.sort[property] });
                }
            }
            return request;
        },
        url: "/api/FooApi"
    });
public class FooApiController : ApiController
{

    [HttpPost]
    public BootgridResponseData<FooModel> Get(BootgridRequestData model)
    {
        // This would come from some data store, using the request params...
        // I use PagedList to make pagination easier...
        IPagedList<FooModel> itemsPaged = store.GetPagedFoo();

        // Then return the response with data...
        return new BootgridResponseData<FooModel>()
        {
            current = model.current,
            rowCount = model.rowCount,
            rows = itemsPaged,
            total = itemsPaged.TotalItemCount
        };
    }
}
然后我在API中创建了我的post操作,如下所示:

var grid = $("#my-grid").bootgrid({
        ajax: true,
        rowCount: 10,
        ajaxSettings: {
            method: "POST",
            cache: true
        },
        requestHandler: function (request) {
            // Scan the original sort object from Bootgrid...
            // and populate an array of "SortData"...
            request.sortItems = [];
            if (request.sort == null)
                return request;
            for (var property in request.sort) {
                if (request.sort.hasOwnProperty(property)) {
                    request.sortItems.push({ Field: property, Type: request.sort[property] });
                }
            }
            return request;
        },
        url: "/api/FooApi"
    });
public class FooApiController : ApiController
{

    [HttpPost]
    public BootgridResponseData<FooModel> Get(BootgridRequestData model)
    {
        // This would come from some data store, using the request params...
        // I use PagedList to make pagination easier...
        IPagedList<FooModel> itemsPaged = store.GetPagedFoo();

        // Then return the response with data...
        return new BootgridResponseData<FooModel>()
        {
            current = model.current,
            rowCount = model.rowCount,
            rows = itemsPaged,
            total = itemsPaged.TotalItemCount
        };
    }
}
然后我甚至可以使用我原来的SortData助手类:

public class SortData
{
    public string Field { get; set; } // FIeld Name
    public string Type { get; set; } // ASC or DESC
}
方法1。 考虑你有列的COL1,COL2,COL3,… 您可以使用:

public ActionType Get(int current, int rowCount, Sort sort, string searchPhrase) {
    //sort.col1 == 'asc' (consider sorted by col1 in ascending order)
}

public class Sort
{
    public string col1 { get; set; }
    public string col2 { get; set; }
    public string col3 { get; set; }
    //... other columns
}
方法2。 您可以使用删除参数和手动解析请求数据。我在这里用post而不是get

[HttpPost]
public object Post(){
    string jsonContent = Request.Content.ReadAsStringAsync().Result;
    Dictionary<string, string> keyvalues = new Dictionary<string, string>();
    string[] keyvalue_strings = jsonContent.Split('&');
    string sort_column = "";
    string sort_direction = "";
    for (var i = 0; i< keyvalue_strings.Length; i++)
    {
        var a = keyvalue_strings[i].Split('=');
        a[0] = a[0].Replace("%5B", "[").Replace("%5D", "]");
        keyvalues.Add(a[0], (a[1]));
        if (a[0].Contains("sort"))
        {
            sort_column = a[0].Replace("sort[", "").Replace("]", "");
            sort_direction = a[1];
        }
    }
    //now you have keyvalues, sort_column, sort_direction.
    //...
}
方法1。 考虑你有列的COL1,COL2,COL3,… 您可以使用:

public ActionType Get(int current, int rowCount, Sort sort, string searchPhrase) {
    //sort.col1 == 'asc' (consider sorted by col1 in ascending order)
}

public class Sort
{
    public string col1 { get; set; }
    public string col2 { get; set; }
    public string col3 { get; set; }
    //... other columns
}
方法2。 您可以使用删除参数和手动解析请求数据。我在这里用post而不是get

[HttpPost]
public object Post(){
    string jsonContent = Request.Content.ReadAsStringAsync().Result;
    Dictionary<string, string> keyvalues = new Dictionary<string, string>();
    string[] keyvalue_strings = jsonContent.Split('&');
    string sort_column = "";
    string sort_direction = "";
    for (var i = 0; i< keyvalue_strings.Length; i++)
    {
        var a = keyvalue_strings[i].Split('=');
        a[0] = a[0].Replace("%5B", "[").Replace("%5D", "]");
        keyvalues.Add(a[0], (a[1]));
        if (a[0].Contains("sort"))
        {
            sort_column = a[0].Replace("sort[", "").Replace("]", "");
            sort_direction = a[1];
        }
    }
    //now you have keyvalues, sort_column, sort_direction.
    //...
}

实际上,我更喜欢简单的参数而不是复杂的模型来绑定数据,我只是不知道该用什么。有人告诉我,用字典作为参数不是一件好事,我认为用字典是合适的。无论如何,我会尝试使用,我真的不知道为什么它不应该是一个好的选择。如果行得通,我接受你的回答。谢谢你的帮助!不幸的是,它对我不起作用,我尝试了GET和POST请求。我还尝试将参数设置为KeyValuePair,但没有成功。我的应用程序配置了OData,我知道它提供了不同的模型绑定器。也许可以看看配置OData,看看这是否有助于传递字典?安装软件包Microsoft.AspNet.OData安装软件包Microsoft.AspNet.WebApi.OData实际上,我更喜欢使用简单的参数而不是复杂的模型来绑定数据,我只是不知道该使用什么。有人告诉我,用字典作为参数不是一件好事,我认为用字典是合适的。无论如何,我会尝试使用,我真的不知道为什么它不应该是一个好的选择。如果行得通,我接受你的回答。谢谢你的帮助!不幸的是,它对我不起作用,我尝试了GET和POST请求。我还尝试将参数设置为KeyValuePair,但没有成功。我的应用程序配置了OData,我知道它提供了不同的模型绑定器。也许看看配置
看看这是否有助于传递字典?安装软件包Microsoft.AspNet.OData安装软件包Microsoft.AspNet.WebApi.ODataI我将测试它,并让您知道它是否工作,提前感谢!我会测试它,让你知道它是否有效,提前谢谢!