我正在通过 jquery ajax post 将 json 数据发送到我的 Controller 操作。我的操作中的 IEnumerable 始终为空。
我的 json 是错误的还是为什么模型绑定(bind)器没有将 json 转换为 IEnumerable?
public ActionResult Update(IEnumerable<Teststep> teststeps)
{
//
}
$.ajax({
url: '@Url.Action("Update", "Teststep")',
type: 'POST',
data: [{ "errortext": "oh something bad happended.", "unitid": "10" }, { "errortext": "you got it man.", "unitid": "20"}],
success: function (response) {
debugger;
if (response.success) {
dlg.dialog("close");
// Update UI
}
else {
// Reload the dialog with the form to show model/validation errors
dlg.html(response);
}
}
});
public class Teststep
{
[HiddenInput(DisplayValue = false)]
public int UnitId { get; set; }
public string ErrorText { get; set; }
// some other props removed for readability
}
最佳答案
为了让集合(数组、ienumerables 等)正确地通过模型绑定(bind)器传递到操作方法,我总是必须在 ajax 调用上设置传统的:true 选项:
$.ajax({
url: '@Url.Action("Update", "Teststep")',
type: 'POST',
traditional: true,
...
关于asp.net-mvc - 模型联编程序不会将 json 转换为 IEnumerable<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10899530/