页面JS部分(name 属性要相同,组成一个数组)
var Array = document.getElementsByName("Prize"); var Parray = document.getElementsByName("Probability"); var option = []; var probability = []; for (var i = 0; i < Array.length; i++) { option[i] = Array[i].value; } for (var j = 0; j < Parray.length; j++) { probability[j] = Parray[j].value; } var Prize = "[" + option + "]"; var Probability = "[" + probability + "]";// 处理传入Controller $.post("/Activity/AddAward", { "activityName": $("#PrizeDrawName").val(), "beginTime": $("#start").val(), "endTime": $("#end").val(), "awardName": Prize, "probability": Probability }, function (data) { layer.msg(data); })
Controller层接收页面数据
string awardName1 = awardName.Replace("[", ""); string awardName2 = awardName1.Replace("]", ""); string[] name = awardName2.Split(','); // Controller层再次处理成标准数组 List<string> newName = new List<string>(); // 奖品名称 string probability1 = probability.Replace("[", ""); string probability2 = probability1.Replace("]", ""); string[] newprobability = probability2.Split(','); List<string> newProbability = new List<string>(); // 奖品概率 foreach (string item in name) { if (!string.IsNullOrEmpty(item)) { newName.Add(item); } } // 去除List中为空的部分并重新组成一个集合 foreach (string item1 in newprobability) { if (!string.IsNullOrEmpty(item1)) { newProbability.Add(item1); } }