使用MVC 3绑定JSON来的IEnumerable [英] Binding JSON to IEnumerable using MVC 3

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

问题描述

我使用MVC 3,我有以下控制器:

I am using MVC 3 and I have the following controller:

[HttpPost]
public ActionResult Suggest(IEnumerable<Connection> connect)
{
    return Json(new { result = true });
}

public class Connection
{
    public long Id { get; set; }
    public string Name { get; set; }
}

我的JQuery的是这样的:

My JQuery looks like:

var connections = $('.on');
var connect = [];

$.each(connections, function (i, item) {
     var span = $(item);

     var id = span.attr('data-entity-id');
     var name = span.attr('data-entity-name');


     connect.push({ Id: id, Name: name });
 });

 $.post('myurl', connect, function (data) {
     $('.result').html(data);
 });

的JSON绑定不使用此code工作。

The JSON binding does not work using this code.

推荐答案

您没有张贴JSON。您需要将字符串化的数据,然后告诉你发送JSON数据服务器。为了JSON字符串化将需要包括克罗克福德的 JSON2 库。 (这是谁发​​明了JSON的家伙。)

You aren't posting JSON. You need to stringify the data and then tell the server you're sending JSON data. In order to stringify JSON you will need to include Crockford's JSON2 library. (That's the guy who invented JSON.)

$.ajax({
    url: "myurl",
    type: "POST",
    data: JSON.stringify({ connect: connect }),
    contentType: 'application/json'
    success: function (data) {
        $('.result').html(data);
    }
});

另外,我觉得你可能需要让你的操作参数列表&LT;连接GT&;连接

这篇关于使用MVC 3绑定JSON来的IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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