我正在通过jquery ajax post发送json数据到我的控制器动作。我的行为IEnumerable始终为null。

我的json是错误的还是模型绑定程序为什么不将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等)正确地通过modelbinder传递给action方法,我一直不得不在ajax调用中设置Traditional:true选项:

$.ajax({
    url: '@Url.Action("Update", "Teststep")',
    type: 'POST',
    traditional: true,
    ...

09-26 00:18