小编典典

POST json字典

c#

我正在尝试以下操作:内部带有字典的模型在第一个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 = 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:

$(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是相同的。我只能假设有些东西在途中迷路了。

有人知道我在做什么错吗?


阅读 333

收藏
2020-05-19

共1个答案

小编典典

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

2020-05-19