0

我正在尝试使用 JQGrid 和 MVC4 实现高级搜索。我的模型的定义似乎很好(如下所示):

public class SearchModel
{
    public string sidx { get; set; } 
    public string sord { get; set; } 
    public int page { get; set; } 
    public int rows { get; set; } 
    public bool _search { get; set; } 
    public string searchField { get; set; } 
    public string searchOper { get; set; } 
    public string searchString { get; set; }
    public FilterModel filters { get; set; }
}
public class FilterModel
{
    public string groupOp { get; set; }
    public List<RuleModel> rules { get; set; }
}
public class RuleModel
{
    public string field { get; set; }
    public string op { get; set; }
    public string data { get; set; }
}

但是,当我运行并搜索 UserName 和 FirstName 时,接收控制器将过滤器显示为 null。然后我检查了(Request.Params)[“filters”],它显示以下文本 -

"{\"groupOp\":\"AND\",\"rules\":[{\"field\":\"UserName\",\"op\":\"eq\",\"data\":\"a\"},{\"field\":\"FirstName\",\"op\":\"eq\",\"data\":\"b\"}]}"

它显示 _search 是真的。我错过了什么吗?

4

2 回答 2

0

I think that the main problem which you have is that filters parameter will be send by jqGrid in another way as all other parameters. You can define it as string and make conversion to FilterModel from JSON manually (using JsonConvert.DeserializeObject<FilterModel>(filters) of Newtonsoft.Json or using serializer.Deserialize<FilterModel>(filters), where JavaScriptSerializer serializer = new JavaScriptSerializer();). It seems to me the most easy way. You can of cause use custom model binder of MVC, but you will have at the end the same results.

Alternatively you can try to change the value of filters parameter which send jqGrid

serializeGridData: function (postData) {
    var dataToSend = $.extend({}, postData); // make copy
    if (dataToSend.filters) {
        dataToSend.filters = $.parseJSON(postData.filters);
    }
    // the last statement can be not needed and one should just use
    //     return dataToSend;
    // instead. If one do convert the data to JSON as in the line below
    // one should use ajaxGridOptions: { contentType: "application/json" }
    // option of jqGrid to set the corresponding ContentType HTTP header
    return JSON.stringify(dataToSend);
}

I don't tested the above code, but I think that it could work.

Additionally I want post some common remarks.

You need implement server side paring and filtering only for really large dataset. In case of having less as 1000 rows of data for example I prefer to use loadone: true parameter and return all data at one to the client. Filtering of such set of data on the client side work really good (and even more quickly as server side filtering) and you will need don't write any server code for filtering. So: don't try to use sever side filtering on small dataset.

The next remark. If you use only Advanced Searching then you can remove searchField, searchOper and searchString parameters. "Simple" searching is legacy model.

The next remark: default names of parameters which send jqGrid is not the best: sidx, sord, _search. One can use prmNames parameter of jqGrid to rename almost all parameters send by jqGrid. For example if you would use prmNames: {sort: "sortIndex", order: "sortDirection", search: "isSearching"} you can rename sidx to sortIndex, sord to sortDirection and _search to isSearching in SearchModel.

The last remark. If you really need to implement server side filtering of data you can consider to implement the full model described here. It includes groups part together with rules. The groups part will be filled if you would use multipleGroup: true option together with multipleSearch: true.

于 2014-09-07T10:09:25.450 回答
0

如果我记得正确,请求包含一个匿名 JSON 对象(没有任何名称),所以在你的控制器中你不能传递一个SearchModel参数,你必须明确添加所有参数:

ActionName (string sidx, string sord, int page, ..., FilterModel filters)

另一种方法是编写自定义模型绑定器(这很简单),它将您的请求参数包装到单个 SearchModel 对象中。

于 2014-09-07T08:38:50.197 回答