2

I am trying to make a post that should use the Default Model Binder functionality in ASP.NET MVC 2 but unfortunately I can't get through....

When I click on the checkout button I populate a form dinamically using jQuery code and then submit this form to the server. This is the form that get submitted

<form action="/x/Order/Checkout" id="cartForm" method="post">
    <input name="__RequestVerificationToken" type="hidden" value="UDjN9RdWheKyWK5Q71MvXAbbDNel6buJd5Pamp/jx39InuyYIQVptcEubIA2W8DMUzWwnZjSGkLspkmDPbsIxy8EVuLvfCSZJJnl/NrooreouptwM/PaBEz2v6ZjO3I26IKRGZPqLxGGfITYqlf8Ow==">
    <input id="CustomerID" name="CustomerID" type="hidden" value="1">
    <input id="FirmID" name="FirmID" type="hidden" value="2">
    <input type="hidden" name="CartItems[0].ServiceTypeID" value="1">
    <input type="hidden" name="CartItems[0].Quantity" value="1">
    <input type="hidden" name="CartItems[1].ServiceTypeID" value="2">
    <input type="hidden" name="CartItems[1].Quantity" value="1">
</form>

This is the jQuery code that handle the submit event for the form

$("#cartForm").submit(function (event) {
    event.preventDefault();
    var form = $("#cartForm");
    var panel = form.parent();
    panel.parent().block();
    $.ajax({
        type: "post",
        dataType: "html",
        url: '<%: Url.Content("~/Order/Checkout") %>',
        async: false,
        data: form.serialize(),
        success: function (response, status, xml) {
            panel.parent().unblock();
        },
        error: function (response) {
            panel.parent().unblock();
        }
    });
});

This is the controller action that should be get called

[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult Checkout( CartModel cart ) {
}

And finally this is the CartModel class involved

public class CartModel : BaseModel
{
    public int CustomerID { get; set; }
    public int FirmID { get; set; }

    public List<CartItemModel> CartItems { get; set; }

    public CartModel() {
        CartItems = new List<CartItemModel>();
    }
}

public class CartItemModel : BaseModel
{
    public int ServiceTypeID { get; set; }
    public int Quantity { get; set; }
}

But the default Model Binder does not bind the web form data to a CartModel class. Using Fiddler I have been able to see that the data sent to the server is correct as you can see from the following snapshot

alt text

Any suggestion?

Lorenzo
  • 29,081
  • 49
  • 125
  • 222
  • 1
    Have you tried stepping through the defaultmodelbinder code and see where it gets stuck binding? – Jaime Dec 28 '10 at 13:46
  • Check out http://stackoverflow.com/questions/1473904/defaultmodelbinder-ilist-vs-list. Maybe using the IList will fix the problem. – Artem Koshelev Dec 28 '10 at 13:58
  • @Jaime: No I did not try at the moment. Simply asked here to see if somebody can note a macro error I dont see :) – Lorenzo Dec 28 '10 at 14:03
  • @redsquare: some default properties. Can this be a reason? I am gonna trying removing the inheritance – Lorenzo Dec 28 '10 at 14:04
  • 1
    @Artem K.: I have seen that question before but I completely agree with the only answer posted `The model binder needs concrete types to bind to. If you tell it to bind to an interface it can't do anything because it can't instantiate an interface to bind to.` – Lorenzo Dec 28 '10 at 14:06
  • 1
    Actually I just copied your code into an empty project and it does work with both IList and List. So, the problem seems to be in BaseModel. – Artem Koshelev Dec 28 '10 at 14:19
  • @Artem K.: you're right. it works even with the interface. never stop learning :) – Lorenzo Dec 28 '10 at 17:58
  • Quote from [this Hanselman post](http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx): "Parameters of type **IEnumerable, ICollection, IList, T[], Collection, and List** are bound using the first syntax" (meaning indexing). Also, both **Dictionary** and **IDictonary** are supported. – Oliver Jul 17 '12 at 05:59

1 Answers1

3

Following @Artem K. comment, the code looks good.

Please verify if the BaseModel class prevents the object being binded. Also verify that you dont have any Filter preventing the request being executed (like Authorize, etc.)

ForrestWhy
  • 486
  • 1
  • 4
  • 9