1

我正在尝试将一些数据数组发布到控制器。我的观点是:

<script>
   $.post("/",{
       person: [
           { id: 1, name: "a" },
           { id: 2, name: "b" }
       ]
   });
</script>

在我的控制器中:

[HttpPost]
public ActionResult Index(List<Person> person)
{
    //something
}

当我检查发送的 http 数据时,我看到数据是:

person[0][id]
person[0][name]
person[1][id]
person[1][name]

但默认模型绑定器的正确方法是:

person[0].id
person[0].name
person[1].id
person[1].name

我该如何解决?

4

1 回答 1

1

您无法在$.post需要使用的情况下执行此操作,$.ajax因为您需要将其设置contentType'application/json'使 mobel binder 对您无法使用的操作感到满意$.post

$.ajax({
        url: '/',
        type: 'POST',
        data: JSON.stringify({
            person: [
                { id: 1, name: "a" },
                { id: 2, name: "b" }
            ]
        }),
        contentType: 'application/json'
    });

而且您还需要JSON.stringify您的数据以使其与模型绑定器一起使用。

于 2012-10-11T20:01:02.100 回答