MVC4 JSON模型绑定不起作用

本文关键字:不起作用 绑定 模型 JSON MVC4 | 更新日期: 2023-09-27 18:04:18

使用以下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}

问题是请求参数为空。我不知道为什么它不工作

MVC4 JSON模型绑定不起作用

我们遇到了同样的问题,然后我们决定使用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将解析该值。我希望它能解决你的问题。