我正在用 [int,bool] 填充数组:
$.each(ownedChecked, function (key, val) {
/*code to set id as int and paintedTrue as bool*/
ownedIDs.push([id,paintedTrue]);
});
我创建了一个将被字符串化的 var:
var saveData = { OwnedListEntryIDs:ownedIDs };
//send data
$.ajax({
url: '/ListEntry/OwnedModels',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(saveData),
success: function (data) { alert('Update success: ' + data); },
failure: function (data) { alert('Update failed: ' + data); }
});
这是视图模型:
public class OwnedPaintedSave
{
//collection contains a ListEntryID and a bool indicating whether it is painted or not
public Dictionary<int,bool> OwnedListEntryIDs { get; set; }
}
进入控制器方法后,ModelState.Isvalid 传递,代码进入 foreach 循环,但模型始终为 NULL:
[HttpPost]
public JsonResult OwnedModels(OwnedPaintedSave models)
{
if (ModelState.IsValid)
{
foreach (var id in models.OwnedListEntryIDs)
{ }
}
}
我尝试在 $.ajax 方法中设置传统:true,但同样的问题。我猜 ViewModel 中的 Dictionary 不是我需要绑定的,但我不确定它应该是什么。谢谢你的帮助。