Default model binder does not bind my model class

发布于 2022-08-25 10:25:10 字数 2731 浏览 17 评论 0

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文