js代码
//传递一个json数组传递给控制器,转换为实体对象的集合
xcaritem.prototype.saveAdd = function () {
var my = this;
var itemlist = [];
var item = "";
var price = "";
var itemjson = "";
var txtinput = $("#appendul li");
var check = true;
var falseHtml = "<span style=\"color:red\">*</span>";
//遍历li,
txtinput.each(function () {
item = $(this).find('input[name="item"]').val();
price = $(this).find('input[name="price"]').val();
if (item != "") {
if (!common.checkNum(price)) {
common.alert("存在不符合要求的价格,请核对后重新提交(价格为非0正整数)");
$(this).append(falseHtml);
check = false;
};
}
if (item != "" && price != "") {
itemjson = { ParamItemName: item, Price: price };
itemlist.push(itemjson);
}
});
if (check) {
$.ajax({
url: my.webRoot + "/ExtendItem/SaveAdd",
type: "post",
//dataType: "json",
data: { "list": JSON.stringify(itemlist) },
success: function (data) {
var dataResult = JSON.parse(data);
common.alert("添加成功", function () {
layer.closeAll();
my.loadPartialView();
});
}
});
}
controller:
#region 保存新增增配项
[HttpPost]
public JsonResult SaveAdd()
{
var list = JsonHelper.JsonToObject<IList<DealerExtendCarIdParamItemEntity>>(RequestUtility.GetFormString("list"));
foreach (var item in list)
{
item.AccountId = base.LoginInfo.DasAccountId;
item.CreateTime = DateTime.Now;
item.DealerId = base.LoginInfo.DealerId;
item.IsActive = 1;
item.LastUpdateAccountId = base.LoginInfo.DasAccountId;
item.UpdateTime = DateTime.Now;
DealerExtendCarIdParamItemBll.GetInstance().Insert(item);
}
var result = new ResResult();
result.IsSuccess = true;
result.ResultMsg = "保存成功";
return Json(JsonHelper.ObjectToJson(result));
}
#endregion