简体   繁体   English

ASP.NET MVC中的模型绑定数组

[英]Model binding array in ASP.NET MVC

I'm using Tablesorter and currently implementing server-side paging using Ajax. 我正在使用Tablesorter ,当前正在使用Ajax实现服务器端分页。 Tablesorter supports sorting on multiple columns and uses the following URL when clicking on the first column: Tablesorter支持对多列进行排序,并在单击第一列时使用以下URL:

http://example.com/tablesorter/json?page=0&size=25&column[0]=1

The above works fine with my (Tablesorter)controller action: 上面的与我的(Tablesorter)控制器动作正常工作:

public JsonResult Json(int page, int size, int[] column) 公共JsonResult Json(int页面,int大小,int []列)

But if I only sort by the second column the following URL is called, which results in column being null. 但是,如果仅按第二列排序,则会调用以下URL,这将导致列为空。 I guess due to a missing zero-index value. 我猜是由于缺少零索引值。

http://example.com/tablesorter/json?page=0&size=25&column[1]=1

So my question is: Can I somehow model bind the given Tablesorter format using some other type or will I have to rewrite Tablesorter's URL format? 所以我的问题是:我可以以某种方式使用其他类型的模型绑定给定的Tablesorter格式,还是必须重写Tablesorter的URL格式?

When sorting by multiple columns the format is: 当按多列排序时,格式为:

http://example.com/tablesorter/json?page=0&size=25&column[0]=1&column[1]=1

You can use the customAjaxUrl option to modify the ajax url however you desire. 您可以根据需要使用customAjaxUrl选项修改ajax网址。

Maybe using jQuery's $.param() function would help? 也许使用jQuery的$.param()函数会有所帮助?

ajaxUrl: 'http://example.com/tablesorter/json?page={page}&size={size}',
customAjaxUrl: function(table, url) {
    // sortList = [[1,0], [2,0]]
    var sort = $.param({ column : table.config.sortList });
    // result: "column[0][]=1&column[0][]=0&column[1][]=2&column[1][]=0"
    return url + '&' + sort;
}

If that format doesn't work, then it might need some server side tweaking? 如果该格式不起作用,则可能需要对服务器端进行一些调整?


If you MUST have all columns defined, try this code: 如果必须定义所有列,请尝试以下代码:

ajaxUrl: 'http://example.com/tablesorter/json?page={page}&size={size}',
customAjaxUrl: function(table, url) {
    var i, v,
    c = table.config,
    s = c.sortList; // sortList = [[1,0], [2,0]]
    for (i = 0; i < c.columns; i++){
        v = 2;
        if ($.tablesorter.isValueInArray(i, s)) {
            $.each(s, function(j){ 
                if (s[j] && s[j][0] === i) {
                    v = s[j][1];
                }
            });
        }
        // 0 = ascending; 1 = descending; 2 = unsorted
        url += '&column[' + i + ']=' + v;
    }
    // result: "&column[0]=2&column[1]=0&column[2]=0&column[3]=2&column[4]=2"
    return url;
}

If that doesn't work, then I'm not sure what to tell you then since I don't know much about ASP.NET & model binding. 如果那行不通,那么我不确定要告诉您什么,因为我对ASP.NET和模型绑定了解不多。 You might want to check out this answer as well. 您可能还想看看这个答案

I know this is an old item, but I worked with Mottie's answer and patched it up a bit. 我知道这是一个旧项目,但是我使用了Mottie的答案并对其进行了修补。

customAjaxUrl: function(table, url) {
    // build sort list
    var config = table.config;
    var sortList = config.sortList;

    for (var i = 0; i < config.columns; i++){
        var v = 2;

        $.each(sortList, function(j, item){ 
            if (item && item[0] === i) {
                v = item[1];
            }
        });

        // 0 = ascending; 1 = descending; 2 = unsorted
        url += '&sortColumn[' + i + ']=' + v;
    }

    // build filter list
    var filterList = config.pager.currentFilters;

    for (var i = 0; i < filterList.length; i++) {
        url += '&filterColumn[' + i + ']=' + filterList[i];
    }

    return url;
},

My MVC action declaration looks like this 我的MVC操作声明如下所示

public ActionResult ListByPage(int page, int size, List<int> sortColumn, List<string> filterColumn)
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM