开发者

JQuery $.post not working properly

开发者 https://www.devze.com 2022-12-26 10:08 出处:网络
Can\'t seem to find a fix for this issue I have the following code on the onclick event of an a html tag:

Can't seem to find a fix for this issue

I have the following code on the onclick event of an a html tag:

AddVacationToCart(  
{  
ServiceSupplier:'Kiki',  
ProductId:'0;11968;0;0;187;1',  
Name:'Excelsior',  
NumberOfStars:'*****',  
TotalPrice:'1620.00',  
PriceLevelName:'Standard',  
Currency:'EUR',  
Status:'',  
StartDate:'2010-06-17',  
EndDate:'2010-06-24',  
NumberOfNights:'7',  
Rooms:[  
{  
NumberOfAdults:'2',  
NumberOfChildren:'0',  
ChildrenAges:[]  
}  
]  
},'0;11968;0;0;187;1');return false;

I also have this code:

function AddVacationToCart(vacation, id) {
            $.post("/ShoppingCart.mvc/AddVacation",
        vacation,
        function(data) {
            var div = $("div[id*=cartv" + id + "]");
            var removeF开发者_Python百科romCartHtml = "Adaugat";
            $(div).html(removeFromCartHtml);
        }, "json");
        }

This is the code in my ShoppingCartController AddVacation Action:

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddVacation(Vacation test)
        {
...
}

The post works as in the (Vacation) test object gets filled with the corresponding properties like ServiceSupplier, ProductId, Name etc. Except the properties of my Rooms field do not get their corresponding values. Any ideeas?


I find that since 1.4 the ajax functions of JQuery are buggy as hell. I have scripts that work fine in every browser on every platform, with the exception of Chrome on Linux. I have filed bug reports to that effect and received no response so far.


Rooms is an array, so you have to name the props linke Rooms[0].NumberOfAdults, Rooms[1].NumberOfAdults to have the default modelbinder working as you expected.


Your code should be like this:

AddVacationToCart(  
{  
  ServiceSupplier:'Kiki',  
  ProductId:'0;11968;0;0;187;1',  
  Name:'Excelsior',  
  NumberOfStars:'*****',  
  TotalPrice:'1620.00',  
  PriceLevelName:'Standard',  
  Currency:'EUR',  
  Status:'',  
  StartDate:'2010-06-17',  
  EndDate:'2010-06-24',  
  NumberOfNights:'7',
  'Rooms[0].NumberOfAdults' :'2',  
  'Rooms[0].NumberOfChildren':'0',  
  'Rooms[0].ChildrenAges[0]': '18'

},'0;11968;0;0;187;1');return false;

Because of ASP.NET MVC binding rules. Just test it

0

精彩评论

暂无评论...
验证码 换一张
取 消