POST json字典 [英] POST json dictionary

查看:150
本文介绍了POST json字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以下内容:一个包含字典的模型在第一个ajax请求上发送它,然后将结果重新序列化并发送回控制器。



这应该测试我可以在我的模型中找回一本字典。它不工作



这是我简单的测试:

  public class HomeController:Controller 
{
public ActionResult Index(T a)
{
return View();
}

public JsonResult A(T t)
{
if(t.Name.IsEmpty())
{
t = new T();
t.Name =myname;
t.D =新字典< string,string>();
t.D.Add(a,a);
t.D.Add(b,b);
t.D.Add(c,c);
}
return Json(t);
}
}

//模型
public class T
{
public string Name {get;组; }
public IDictionary< string,string> D {get;组; }
}

javascript:

  $(function(){
var o = {
名称:'somename',
D:{
一个:b,
b:c,
c:d
}
};

$ .ajax({
url:actionUrl('/ home / a'),
contentType:'application / json',
type:'POST',
success:function结果){
$ .ajax({
url:actionUrl('/ home / a'),
data:JSON.stringify(result),
contentType:'application / json',
type:'POST',
success:function(result){
}
});
}
});
});

在firebug中,json收到并且发送的json是相同的。我只能假设在路上失去的东西。



任何人都有一个想法,我做错了什么?

解决方案

由于 JsonValueProviderFactory 的方式不支持实现的绑定字典。


I'm trying the following : A model with a dictionary inside send it on the first ajax request then take the result serialize it again and send it back to the controller.

This should test that I can get back a dictionary in my model. It doesn't work

Here's my simple test:

public class HomeController : Controller
{
    public ActionResult Index (T a)
    {
      return View();
    }

    public JsonResult A(T t)
    {
      if (t.Name.IsEmpty())
      {
        t = new T();
        t.Name = "myname";
        t.D = new Dictionary<string, string>();
        t.D.Add("a", "a");
        t.D.Add("b", "b");
        t.D.Add("c", "c");
      }
      return Json(t);
    }
}

//model
public class T
{
  public string Name { get; set; }
  public IDictionary<string,string> D { get; set; }
}

The javascript:

$(function () {
    var o = {
        Name: 'somename',
        "D": {
            "a": "b",
            "b": "c",
            "c": "d"
        }
    };

    $.ajax({
        url: actionUrl('/home/a'),
        contentType: 'application/json',
        type: 'POST',
        success: function (result) {
            $.ajax({
                url: actionUrl('/home/a'),
                data: JSON.stringify(result),
                contentType: 'application/json',
                type: 'POST',
                success: function (result) {
                }
            });
        }
    });
});

In firebug the json received and the json sent are identical. I can only assume something gets lost on the way.

Anyone has an idea as to what I'm doing wrong?

解决方案

Due to the way JsonValueProviderFactory is implemented binding dictionaries is not supported.

这篇关于POST json字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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