通常当您使用 MVC ModelBind 对象列表时,操作如下所示:
public ActionResult Hello(List<DocumentViewModel> viewModels)
{
Post 数据看起来像这样:
[0].Id 1
[0].Name Bob
[1].Id 2
[1].Name Jane
但是,我怎样才能让下面的帖子数据起作用呢?
0[Id] 1
0[Name] Bob
1[Id] 2
1[Name] Jane
帖子数据由 Kendo UI 提供,所以我认为我没有很大的灵 active 来更改它。
更新 感谢 Petur 的回答。为了扩展他的答案,这个函数:
common.MvcSerializeArray = function (array, prefix) {
var result = {};
if (!prefix) {
prefix = '';
}
if (array) {
for (var i = 0; i < array.length; i++) {
if ($.isPlainObject(array[i])) {
for (var property in array[i]) {
result[prefix + "[" + i + "]." + property] = array[i][property];
}
} else {
result[prefix + "[" + i + "]"] = array[i];
}
}
}
return result;
};
应该在返回到 Kendo 的数据方法之前包装数组:
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read
.Action("PickerDocuments", "DocumentRepository")
.Data(@<text>function(e) { return MyApp.Common.MvcSerializeArray(@Html.Raw(Json.Encode(Model))); }</text>)))
瞧,我轻松获得了发布数据 MVC 模型绑定(bind):
[0].Id 1
[0].Name Bob
[1].Id 2
[1].Name Jane
最佳答案
关于asp.net-mvc - MVC - 如何使用这种不寻常的发布数据格式对列表和对象进行建模?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18853779/