<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"]);
这是后台处理代码