1. 程式人生 > >MVC Controller接受ajax post傳輸的List<Entity>數據

MVC Controller接受ajax post傳輸的List<Entity>數據

.ajax ring div urn ots 接受 var res 傳輸

接收的實體

 public class AccountInfo
    {
        public int TicketType { get; set; }

        public int TouristType { get; set; }

        public string SpotID { get; set; }

        public int TicketNum { get; set; }
    }

ajax傳輸的數據

          JiLu["JL[" + y + "].TicketType"] = parseInt($(SpotID[y].TypeID).val());
                JiLu[
"JL[" + y + "].TouristType"] = parseInt($(SpotID[y].TouristID).val()); JiLu["JL[" + y + "].SpotID"] = SpotStr; JiLu["JL[" + y + "].TicketNum"] = parseInt($(SpotID[y].NumID).val());



$.ajax({
url: "/SalesTicket/Single/AccountMoney",
dateType: ‘json‘,
type: ‘Post‘,
data: JiLu,
async: false,
success: function (data) {
}
})

{

"JL[0].TicketType":2,"JL[0].TouristType":-1,"JL[0].SpotID":"Spot,2","JL[0].TicketNum":1,

"JL[1].TicketType":1,"JL[1].TouristType":-1,"JL[1].SpotID":"Spot,2,3,1","JL[1].TicketNum":1

}

如果數據格式為 {LJ:[...]} 則controller只可以收到條數,數據為null

而且索引必須為0開頭的自增數,不然接收數據錯亂

後臺Controller

[HttpPost]
        [HandlerAjaxOnly]
        
public ActionResult AccountMoney(List<AccountInfo> JL) { foreach (var x in JL) { } return Json(null); }

技術分享

MVC Controller接受ajax post傳輸的List<Entity>數據