<script type="text/javascript"> $(function () { $("button#ljyd").click(function () { var errorMsg = 0; //會員ID var memberId = $("#memberId").val(); //聯盟套餐Id var unionId = $("#unionHideId").val(); var txt = []; $("div.lmsj_bt.lmyd_0").each(function () { var unionContentId = $(this).attr("data-unionContentId"); var contentCompanyId = $(this).attr("data-contentCompanyId"); var companyName = $(this).attr("data-contentCompanyName"); var tabl = $(this).find("a.dcbj_1 input:hidden"); //選擇的桌子 var tableId = tabl.val() == undefined ? null : tabl.val(); //選擇的菜品 var proIds = ""; $(this).find("div.plus-tag.tagbtn.clearfix a").each(function () { proIds += $(this).attr("value") + ","; }); if (proIds.length != 0) { proIds = proIds.substring(0, proIds.length - 1); } //選擇的時間 var time = $(this).find("#datepicker").val() + " " + $(this).find("select").val(); if ($(this).find("#datepicker").val() == "" || $(this).find("select").val() == "") { //new Boxy.alert(companyName + "的預定時間必須選擇完整", null, { title: "提示信息" }); errorMsg = 1; return; } //預定人數 var people = $(this).find("div.lmsj_renshu.float_2 input").val(); if (people == "") { //new Boxy.alert(companyName + "的預定人數必須填寫", null, { title: "提示信息" }); errorMsg = 2; return; } //alert(unionContentId + " " + contentCompanyId + " " + tableId + " " + proIds + " " + time + " " + people); var obj = { "unionContentId": unionContentId, "contentCompanyId": contentCompanyId, "tableId": tableId, "proIds": proIds, "time": time, "people": people }//構造Json txt.push(obj); }); //聯系人 var ydpepole = $("#ydmemberName").val(); //聯系人電話 var ydpeoplephone = $("#ydmemberPhone").val(); if (errorMsg==0) { $.ajax({ type: "get", url: "/CompanyZxyd/UnionNewAdd", data: "UnionContent=" + JSON.stringify(txt) + "&memberId=" + memberId + "&unionId=" + unionId + "&ydpepole=" + ydpepole + "&ydpeoplephone=" + ydpeoplephone, success: function (msg) { new Boxy.alert(msg, null, { title: "提示信息" }); //預定成功后的操作 } //操作成功后的操作!msg是后台傳過來的值 }); } else { if (errorMsg==1) { new Boxy.alert( "預定時間必須選擇完整", null, { title: "提示信息" }); } else { new Boxy.alert("預定人數必須填寫", null, { title: "提示信息" }); } } }); }) </script>
這是前台構造json對象的代碼
[DataContract]//構造一個對象模型 class Person { [DataMember] public string unionContentId { get; set; } [DataMember] public string contentCompanyId { get; set; } [DataMember] public string tableId { get; set; } [DataMember] public string proIds { get; set; } [DataMember] public string time { get; set; } [DataMember] public string people { get; set; } } /// <summary> /// JSON序列化和反序列化輔助類 /// </summary> public class JsonHelper { /// <summary> /// JSON序列化 /// </summary> public static string JsonSerializer<T>(T t) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); MemoryStream ms = new MemoryStream(); ser.WriteObject(ms, t); string jsonString = Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return jsonString; } /// <summary> /// JSON反序列化 /// </summary> public static T JsonDeserialize<T>(string jsonString) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); T obj = (T)ser.ReadObject(ms); return obj; } } //調用方法 List<Person> outPerson = new List<Person>(); outPerson = JsonHelper.JsonDeserialize<List<Person>>(Request.QueryString["UnionContent"]);
這是后台處理代碼