Session版购物车+MVC局部刷新
效果图:
加载购物车列表(局部视图)
function LoadExpenseDetailsList() { $("#tbExpenseDetailsList tbody").load("/Payment/LoadExpenseDetailsList"); }
<table class="table table-bordered table-striped table-condensed table-hover fixheader" id="tbExpenseDetailsList"> <thead> <tr> <th> Operate </th> <th> number </th> <th> Expense Items </th> <th> Expense Items Detail </th> <th> Product Name </th> <th> Purchase Date </th> <th> Explain </th> <th> Purchaser </th> <th> Payment Date </th> <th> Amount </th> <th> Currency </th> <th> Note </th> </tr> </thead> <tbody> </tbody> </table>
@using Web.Models; @using System.Data; @{ List<ExpenseDetails> list = new List<ExpenseDetails>(); if (Session["ExpenseDetailsList"] != null) { list = Session["ExpenseDetailsList"] as List<ExpenseDetails>; } foreach (ExpenseDetails item in list) { <tr> <td> <a id="" onclick="RemoveExpenseDetailsList(@item.id)" style="cursor:pointer">Remove</a> </td> <td> @item.id <input type="hidden" name="id" value="@item.id" /> </td> <td> @item.expenseItems <input type="hidden" name="ExpenseItems" value="@item.expenseItems" /> </td> <td> @item.expenseItemsDetail <input type="hidden" name="ExpenseItemsDetail" value="@item.expenseItemsDetail" /> </td> <td> @item.productName <input type="hidden" name="ProductName" value="@item.productName" /> </td> <td> @if (string.IsNullOrEmpty(item.purchaseDate.ToString())) { @item.purchaseDate } else { @Convert.ToDateTime(item.purchaseDate).ToString("yyyy-MM-dd") } <input type="hidden" name="PurchaseDate" value="@item.purchaseDate" /> </td> <td> @item.explain <input type="hidden" name="Explain" value="@item.explain" /> </td> <td> @item.purchaser <input type="hidden" name="Purchaser" value="@item.purchaser" /> </td> <td> @if (string.IsNullOrEmpty(item.paymentDate.ToString())) { @item.paymentDate } else { @Convert.ToDateTime(item.paymentDate).ToString("yyyy-MM-dd") } <input type="hidden" name="PaymentDate" value="@item.paymentDate" /> </td> <td> @item.amount <input type="hidden" name="Amount" value="@item.amount" /> </td> <td> @item.currency <input type="hidden" name="Currency" value="@item.currency" /> </td> <td> @item.note <input type="hidden" name="Currency" value="@item.note" /> </td> </tr> } }
/// <summary> /// 加载购物车列表数据(显示Session数据) /// </summary> public PartialViewResult LoadExpenseDetailsList() { return PartialView("_ExpenseDetailsList"); }
Add to(添加到购物车列表)
function AddToExpenseDetails() { var ExpenseItems = $("#ExpenseItems").find("option:selected").text(); var ExpenseItemsDetail = $("#ExpenseItemsDetail").find("option:selected").text(); var ProductName = $("#ProductName").val(); var PurchaseDate = $("#PurchaseDate").val(); var Explain = $("#Explain").val(); var Purchaser = $("#Purchaser").find("option:selected").text(); var PaymentDate = $("#PaymentDate").val(); var Amount = $("#Amount").val(); var Currency = $("#Currency").text(); var Note = $("#Note").val(); if (Currency == "") { AlertMsg("AddToExpenseDetails", "Failure", "Withdrawal account can not be empty!"); return; } if (Purchaser == "") { AlertMsg("AddToExpenseDetails", "Failure", "Purchaser can not be empty!"); return; } if (PaymentDate == "") { AlertMsg("AddToExpenseDetails", "Failure", "Payment Date can not be empty!"); return; } if (PurchaseDate == "") { AlertMsg("AddToExpenseDetails", "Failure", "Purchase Date can not be empty!"); return; } if (Explain == "") { AlertMsg("AddToExpenseDetails", "Failure", "Explain can not be empty!"); return; } if (Amount == "") { AlertMsg("AddToExpenseDetails", "Failure", "Amount can not be empty!"); return; } $.post("/Payment/AddExpenseDetailsInfo", { ExpenseItems: ExpenseItems, ExpenseItemsDetail: ExpenseItemsDetail, ProductName: ProductName, PurchaseDate: PurchaseDate, Explain: Explain, Purchaser: Purchaser, PaymentDate: PaymentDate, Amount: Amount, Currency: Currency, Note: Note }, function (data) { if (data == "Yes") { $("#ExpenditureAccount").attr("disabled", true); $("#ExpenseNote").attr("disabled", true); $("#Purchaser").attr("disabled", true); $("#PaymentDate").attr("disabled", true); $("#Explain").attr("disabled", true); $("#PurchaseDate").attr("disabled", true); $("#ProductName").val(""); $("#Amount").val(""); $("#Note").val(""); LoadExpenseDetailsList(); } }); }
/// <summary> ///类似 添加购物车 /// </summary> [HttpPost] public string AddExpenseDetailsInfo(string ExpenseItems, string ExpenseItemsDetail, string ProductName, DateTime PurchaseDate, string Explain, string Purchaser, DateTime PaymentDate, decimal Amount, string Currency, string Note) { try { List<ExpenseDetails> list = new List<ExpenseDetails>(); if (Session["ExpenseDetailsList"] != null) { list = Session["ExpenseDetailsList"] as List<ExpenseDetails>; } ExpenseDetails entity = new ExpenseDetails(); entity.id = list.Count + 1; entity.expenseItems = ExpenseItems; entity.expenseItemsDetail = ExpenseItemsDetail; entity.productName = ProductName; entity.purchaseDate = PurchaseDate; entity.explain = Explain; entity.purchaser = Purchaser; entity.paymentDate = PaymentDate; entity.amount = Amount; entity.currency = Currency; entity.note = Note; list.Add(entity); //decimal total = 0; for (int i = 0; i < list.Count; i++) { list[i].id = i + 1; //total += list[i].amount; } //ViewData["Total"] = total; Session["ExpenseDetailsList"] = list; return "Yes"; } catch (Exception ex) { return "No"; } }
Remove(删除购物车数据)
function RemoveExpenseDetailsList(id) { $.post("/Payment/DelExpenseDetailsInfo", { id: id }, function (data) { if (data == "empty") { $("#ExpenditureAccount").attr("disabled", false); $("#ExpenseNote").attr("disabled", false); $("#Purchaser").attr("disabled", false); $("#PaymentDate").attr("disabled", false); $("#Explain").attr("disabled", false); $("#PurchaseDate").attr("disabled", false); } LoadExpenseDetailsList(); }); }
/// <summary> /// 根据ID删除购物车列表数据(删除Session值) /// </summary> [HttpPost] public string DelExpenseDetailsInfo(int id) { if (id != 0)//id=0 清空购物车(Remove All) { List<ExpenseDetails> list = new List<ExpenseDetails>(); if (Session["ExpenseDetailsList"] != null) { list = Session["ExpenseDetailsList"] as List<ExpenseDetails>; var item = list.Where(w => w.id == id).FirstOrDefault(); if (item != null) { list.Remove(item); if (list.Count > 0) { for (int i = 0; i < list.Count; i++) { list[i].id = i + 1; } Session["ExpenseDetailsList"] = list; } else { Session["ExpenseDetailsList"] = null; } } } } else { Session["ExpenseDetailsList"] = null; } return Session["ExpenseDetailsList"] == null ? "empty" : "have"; }
Save All(保存所有数据到数据库)
function SaveAll() { var Issued = $("#Issued").val(); var ExpenditureAccountID = $("#ExpenditureAccount").children('option:selected').val(); var ExpenditureAccountInfo = $("#ExpenditureAccount").find("option:selected").text(); var Explain = $("#Explain").children('option:selected').val(); var Purchaser = $("#Purchaser").children('option:selected').val(); var PaymentDate = $("#PaymentDate").val(); var Note = $("#ExpenseNote").val(); if (ExpenditureAccountID == "") { AlertMsg("SaveAll", "Failure", "Withdrawal accounts can not be empty!"); return; } if (Purchaser == "") { AlertMsg("SaveAll", "Failure", "Purchaser can not be empty!"); return; } $("#SaveAll").attr("disabled", true); $.post("/Payment/SaveAllExpenseDetails", { Issued: Issued, PaymentType: "Expense", ExpenditureAccountID: ExpenditureAccountID, ExpenditureAccountInfo: ExpenditureAccountInfo, Purchaser: Purchaser, Explain: Explain, PaymentDate: PaymentDate, Note: Note }, function (data) { if (data == "true") { AlertMsg("SaveAll", "Success", "Save all success!"); $("#ExpenditureAccount").attr("disabled", false); $("#ExpenseNote").attr("disabled", false); $("#Purchaser").attr("disabled", false); $("#PaymentDate").attr("disabled", false); $("#Explain").attr("disabled", false); $("#PurchaseDate").attr("disabled", false); $("#Currency").text(""); $("#Purchaser").val(""); $("#Explain").val(""); $("#PaymentDate").val(""); $("#PurchaseDate").val(""); $("#tbExpenseDetailsList tbody").html(""); $("#ExpenseNote").val(""); GetBankAccount(""); } else if (data == "empty") { AlertMsg("SaveAll", "Failure", "No data!"); } else if (data == "false") { AlertMsg("SaveAll", "Failure", "Save all failure!"); } else { AlertMsg("SaveAll", "Failure", data); } $("#SaveAll").attr("disabled", false); }); }
/// <summary> /// 保存购物车中所有数据到数据库(添加Session值到数据库) /// </summary> [HttpPost] public string SaveAllExpenseDetails(string Issued, string PaymentType, int? ExpenditureAccountID, string ExpenditureAccountInfo, string Purchaser, string PaymentDate, string Note, string Explain) { string CreateBy = Issued; DateTime CreateDate = DateTime.Now; string sql = ""; int? AccountID = ExpenditureAccountID; string AccountName = ""; string AccountNumber = ""; string Currency = ""; decimal Balance = 0; decimal ExpenditureAmount = 0; List<ExpenseDetails> list = new List<ExpenseDetails>(); list = Session["ExpenseDetailsList"] as List<ExpenseDetails>; if (list != null && list.Count > 0) { foreach (ExpenseDetails item in list) { ExpenditureAmount += item.amount; } } else { return "empty"; } if (!string.IsNullOrEmpty(ExpenditureAccountInfo)) { string[] strExpenditure = ExpenditureAccountInfo.Split('-'); AccountName = strExpenditure[0]; AccountNumber = strExpenditure[2]; Balance = Convert.ToDecimal(strExpenditure[3]) - Convert.ToDecimal(ExpenditureAmount); Currency = strExpenditure[4]; } if (Balance > 0) { sql += @"INSERT INTO dbo.Payments(PaymentType, AccountID, AccountName, AccountNumber, Expense, Income, Currency, Balance, Explain, Purchaser, PaymentDate, Note, CreateDate, CreateBy) VALUES("; sql += "N'" + PaymentType + "',"; sql += "N'" + AccountID + "',"; sql += "N'" + AccountName + "',"; sql += "N'" + AccountNumber + "',"; sql += "N'" + ExpenditureAmount + "',"; sql += "N'" + 0 + "',"; sql += "N'" + Currency + "',"; sql += "N'" + Balance + "',"; sql += "N'" + Explain + "',"; sql += "N'" + Purchaser + "',"; sql += "N'" + PaymentDate + "',"; sql += "N'" + Note + "',"; sql += "N'" + CreateDate + "',"; sql += "N'" + CreateBy + "'"; sql += @") "; sql += @"UPDATE dbo.BankAccount SET Balance=" + Balance + " WHERE ID=" + AccountID + @" "; } else { return "Sorry, your credit is running low"; } try { if (SQLHelper.ExecuteNonQuery(sql) > 0) { sql = ""; int PaymentID = SQLHelper.ExecuteScalar("SELECT TOP(1) PaymentID FROM dbo.Payments ORDER BY CreateDate DESC"); foreach (ExpenseDetails item in list) { sql += @"INSERT INTO dbo.ExpenseDetails(PaymentID, ExpenseItems, ExpenseItemsDetail, ProductName, PurchaseDate, Explain, Purchaser, PaymentDate, Amount, Currency, Note, CreateDate, CreateBy) VALUES("; sql += "N'" + PaymentID + "',"; sql += "N'" + item.expenseItems + "',"; sql += "N'" + item.expenseItemsDetail + "',"; sql += "N'" + item.productName + "',"; sql += "N'" + item.purchaseDate + "',"; sql += "N'" + item.explain + "',"; sql += "N'" + item.purchaser + "',"; sql += "N'" + item.paymentDate + "',"; sql += "N'" + item.amount + "',"; sql += "N'" + item.currency + "',"; sql += "N'" + item.note + "',"; sql += "N'" + CreateDate + "',"; sql += "N'" + CreateBy + "'"; sql += @") "; } if (SQLHelper.ExecuteNonQuery(sql) > 0) { Session["ExpenseDetailsList"] = null; return "true"; } else { return "false"; } } else { return "false"; } } catch (Exception) { return "false"; } }
作者:长毛象
本文版权归作者和博客园共有,个人学习成果,请多多指教,欢迎转载,请保留原文链接