MVC4 JSON模型绑定不起作用 [英] MVC4 JSON Model Binding doesn't work

查看:114
本文介绍了MVC4 JSON模型绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下视图模型:

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)
        {
         }

和发送请求的方式如下:

and send the request the following way:

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请求如下:

The http request looks like this:

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

的问题是,在请求参数为空。我不知道为什么它不能正常工作。

The problem is that the request parameter is null. I have no idea why it doesn't work.

推荐答案

我们有同样的问题,然后我们决定使用CustomModelBinder。我分享我们所做的一切,

We had the same problem, then we decided to use CustomModelBinder. Am sharing what we have done,

在客户端,

在ajaxRequest u需要将整个视图模型如下,

In ajaxRequest u need to send the entire ViewModel as below,

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

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

then stringify the model and assign it to the data,

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

在服务器端,

ü可以用customModelBinder以检索值回

U can use customModelBinder to retrive the value back

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

和在乌拉圭回合视图模型ü需要添加属性,

And in ur viewModel u need to add the attribute,

[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将解析值。我希望这本书能解决乌尔概率。

Now, CustomJsonModelBinder will parse the value. I hope it will solve ur prob.

这篇关于MVC4 JSON模型绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆