-1

我有一个带有属性的模型,其中一个是集合。

我想要一个客户端 JS 函数,该函数用一些值设置该集合(或通过单独的参数或通过我不太熟悉的模型绑定器将其发送到控制器操作)。

这怎么可能?

4

1 回答 1

1

您可以使用 JSON 发送任意复杂的结构:

$.ajax({
    url: '/somecontroller/someaction',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({
        id: 123,
        items: [
            { foo: 'bar 1' },
            { foo: 'bar 2' },
            { foo: 'bar 3' }
        ]
    }),
    success: function(result) {
        alert('ok');
    }
});

并且您的控制器操作将采用与 JSON 结构匹配的视图模型:

[HttpPost]
public ActionResult SomeAction(MyViewModel model)
{
    ...
}

这是与此示例结构匹配的模型定义:

public class MyViewModel
{
    public int Id { get; set; }
    public ItemViewModel[] Items { get; set; }
}

public class ItemViewModel
{
    public string Foo { get; set; }
}
于 2013-06-07T12:40:59.983 回答