Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model binder does not convert json to IEnumerable<T>

I am sending json data to my controller action via jquery ajax post. The IEnumerable in my action is alway null.

Is my json wrong or why does the model binder not convert the json to the 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   

}
like image 463
Elisabeth Avatar asked Mar 22 '26 12:03

Elisabeth


2 Answers

In order to get collections (arrays, ienumerables, etc) to pass correctly through the modelbinder to the action method, I've always had to set the traditional: true option on the ajax call:

$.ajax({
    url: '@Url.Action("Update", "Teststep")',
    type: 'POST',
    traditional: true,
    ...
like image 192
danludwig Avatar answered Mar 24 '26 10:03

danludwig


Now it works! I get 1 item in the IEnumerable. The problem was the messed up json ;-)

 var data = { teststeps: [{ ErrorText: 'bla', UnitId: 10}] };
        $.ajax({
            url: '@Url.Action("Update", "Teststep")',
            type: 'POST',
            data: JSON.stringify(data),
            dataType: 'json',
            contentType: 'application/json'            
        });

[HttpPost]
public ActionResult Update(IEnumerable<Teststep> teststeps)
{

}
like image 37
Elisabeth Avatar answered Mar 24 '26 09:03

Elisabeth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!