POST json 字典 [英] POST json dictionary

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

问题描述

我正在尝试以下操作:内部带有字典的模型在第一个 ajax 请求上发送它,然后将结果再次序列化并将其发送回控制器.

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

这是我的简单测试:

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

javascript:

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

在 firebug 中,接收到的 json 和发送的 json 是相同的.我只能假设有些东西在途中丢失了.

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?

推荐答案

由于 JsonValueProviderFactory 已实现,不支持绑定字典.

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

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

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