1

我设法创建了一个 ApiController 从我的存储库中检索数据,并通过带有 Ajax 的 Bootgrid在我的视图中填充了一个网格。这是发送到 Api 操作的请求数据的示例,由他们的 Docs here给出(查找 POST Body Request 选项卡):

current=1&rowCount=10&sort[sender]=asc&searchPhrase=&id=b0df282a-0d67-40e5-8558-c9e93b7befed

这是一个示例网址:

http://localhost/api/SomeController?current=1&rowCount=10&sort%5BName%5D=asc&searchPhrase=&id=b0df282a-0d67-40e5-8558-c9e93b7befed

我创建了两个 Helper 类来处理我必须作为响应返回的数据,并对数据进行排序(因为它是一个数组):

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...
    }

该方法被调用并且所有参数都正确地带有数据,除了排序,它始终为空。

我应该期待什么样的参数?我也试过放object,但无论如何它都是空的。

4

4 回答 4

2

在了解了更多之后,我看到 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"
    });

然后我在 API 中创建了我的 post 操作,如下所示:

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
        };
    }
}

我的BootgridResponseData问题中已经显示了。我刚刚添加了一个BootgridRequestData以下结构:

public class BootgridRequestData
{
    public int current { get; set; }
    public int rowCount { get; set; }
    public string searchPhrase { get; set; }
    public IEnumerable<SortData> sortItems { get; set; }
}

然后我什至可以使用我原来的SortData辅助类:

public class SortData
{
    public string Field { get; set; } // FIeld Name
    public string Type { get; set; } // ASC or DESC
}
于 2016-08-10T06:23:27.163 回答
1

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

class TestSort
{
   public string field1 { get; set; }
   public string field2 { get; set; }
   ...
}

我在我的网络服务中使用这个类作为排序选项参数。此类中由 bootgrid 选项引用的所有字段都设置为“ASC”或“DESC”。其他保持为空。

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

于 2016-02-10T18:06:47.417 回答
1

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

我最终尝试了一个 Dictionary 对象,它可以工作。

这是我的签名:

[HttpPost]
public async Task<ActionResult> GetActiveDogs(int? current, int? rowCount, 
                Dictionary<string, string> sort, string searchPhrase = null)
于 2016-01-27T22:55:12.023 回答
0

接近 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。 您可以使用删除参数并手动解析请求数据。我在这里使用帖子而不是获取。

[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.
    //...
}
于 2019-06-06T14:00:09.267 回答