MVC4 JSON模型绑定不起作用

时间:2014-05-21 09:59:29

标签: c# jquery ajax json asp.net-mvc-4

使用以下Viewmodel:

public class GetUsersFromNextCircuitRequest
    {
        public int? dosarId { get; set; }
        public List<int> dosareIds { get; set; }
        public string query { get; set; }
        public int page { get; set; }
        public int page_limit { get; set; }
    }

并在以下操作中使用它:

 public ActionResult GetUsersFromNextCircuit(GetUsersFromNextCircuitRequest requestNextUsers)
        {
         }

并按以下方式发送请求:

ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
                    url: self.attr('getusersfromnextcircuiturl'),
                    dataType: "json",
                    contentType: 'application/json; charset=utf-8',
                    type: 'POST',
                    traditional: true,
                    data: function (term, page) {
                        if (self.attr('dosare') !== undefined && self.attr('dosare').length > 0) {
                            var dosareIds = [];
                            self.attr('dosare').forEach(function (element, index, list) {
                                dosareIds.push(element.attr('Id'));
                            });
                            return JSON.stringify({
                                    query: term, // search term
                                    page: page,
                                    dosareIds: dosareIds,
                                    page_limit: 30

                            });
                        }
                        else
                            return JSON.stringify({
                                    query: term, // search term
                                    page: page,
                                    dosarId: self.attr('dosarid'),
                                    page_limit: 30

                            });
                    },
                    results: function (data, page) {
                        var more = (page * 30) < data.total; // whether or not there are more results available
                        return { results: data.users, more: more };
                    }
                }

http请求如下所示:

{"query":"xa","page":1,"dosareIds":[4137,4163],"pagelimit":30}

问题是request参数为null。我不知道它为什么不起作用。

1 个答案:

答案 0 :(得分:1)

我们遇到了同样的问题,然后我们决定使用CustomModelBinder。我正在分享我们所做的事情,

在客户端,

在ajaxRequest中,你需要发送整个ViewModel,如下所示,

        var viewModel = new Object();
        viewModel.query= term;
        viewModel.page= page;
        viewModel.dosareIds= new Array();
        viewModel.dosarIds = dosareIds;
        viewModel.page_limit = 30;

然后对模型进行字符串化并将其分配给数据

data: { viewModel: JSON.stringify(viewModel) };

在服务器端,

你可以使用customModelBinder重新获取值

public class CustomJsonModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        try
        {
            var data = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            JavaScriptSerializer js = new JavaScriptSerializer();
            var temp = js.Deserialize(data.AttemptedValue, bindingContext.ModelType);
            return temp;
        }
        catch
        {
            return null;
        }
    }
}

在你的viewModel中你需要添加属性

[ModelBinder(typeof(CustomJsonModelBinder))]
public class GetUsersFromNextCircuitRequest
{
    public int? dosarId { get; set; }
    public List<int> dosareIds { get; set; }
    public string query { get; set; }
    public int page { get; set; }
    public int page_limit { get; set; }
}

现在,CustomJsonModelBinder将解析该值。我希望它能解决你的问题。