【发布时间】:2020-01-28 11:59:52
【问题描述】:
我有一个添加账单详细信息的表单,如屏幕截图所示:
在此表单中,人首先通过此代码添加存储在会话中的条目:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(string nameValueInsert, string nameValueSubmit, CreateBillViewModel model, int? Patient_id)
{
int count = 0;
var button = nameValueInsert ?? nameValueSubmit;
if (button == "Insert")
{
if (Session["templist"] == null)
{
List<PatientViewModel> lst = new List<PatientViewModel>();
lst.Add(new PatientViewModel()
{ Count = count,
PatientAppointmentID = Int32.Parse(Request.Form["PatientAppointmentID"]),
// BillNo = Request.Form["BillNo"],
Amount = double.Parse(Request.Form["Amount"]),
Description = Request.Form["Description"]
});
Session["templist"] = lst;
}
else
{
List<PatientViewModel> lst = (List<PatientViewModel>)Session["templist"];
lst.Add(new PatientViewModel()
{
Count = lst.Count + 1,
PatientAppointmentID = Int32.Parse(Request.Form["PatientAppointmentID"]),
// BillNo = Request.Form["BillNo"],
Amount = double.Parse(Request.Form["Amount"]),
Description = Request.Form["Description"]
});
Session["templist"] = lst;
}
}
else
{
if (ModelState.IsValid)
{
string username = "";
HttpCookie cookie = HttpContext.Request.Cookies["AdminCookies"];
if (cookie != null)
{
username = Convert.ToString(cookie.Values["UserName"]);
}
tblPatientBill patientbill = new tblPatientBill();
patientbill.PatientAppointmentID = model.PatientAppointmentID;
// patientbill.BillNo = model.ID;
patientbill.Amount = model.AmountTotal;
patientbill.Description = model.Note;
patientbill.Discount = model.Discount;
patientbill.CreatedAt = model.CreatedAt;
patientbill.CreatedBy = username;
patientbill.is_active = true;
db.tblPatientBills.Add(patientbill);
db.SaveChanges();
int PatientBill_ID = Convert.ToInt32(patientbill.ID);
List<PatientViewModel> lst = (List<PatientViewModel>)Session["templist"];
if (lst != null)
{
tblPatientBillDetail billdetail = new tblPatientBillDetail();
foreach (var item in lst)
{
billdetail.PatientBillID = PatientBill_ID;
billdetail.Amount = item.Amount;
billdetail.CreatedAt = DateTime.UtcNow;
billdetail.CreatedBy = username;
billdetail.Description = item.Description;
billdetail.is_active = true;
db.tblPatientBillDetails.Add(billdetail);
db.SaveChanges();
}
Session.Clear();
}
return RedirectToAction("Print", new { Billid = @PatientBill_ID });
}
}
return View(model);
}
}
}
当所有条目提交完成后,所有记录都会保存到数据库中。
现在我要做的是,如果在输入条目时,如果用户想要删除任何行,他应该能够从会话中删除它,并且在最终确定剩余数据后应该进入我正在执行的数据库中以下方法: Id 将通过 Ajax 调用到达控制器:
$("body").on("click", "#tblBills .Delete", function () {
if (confirm("Do you want to delete this row?")) {
var row = $(this).closest("tr");
var Id = row.find("span").html();
console.log("Id");
$.ajax({
type: "POST",
url: "/Session_Delete",
data: '{ID: ' + Id+ '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("hello");
$("#test").remove();
}
});
}
});
在控制器中,我正在从会话中删除该行,如下所示:
[HttpPost]
public ActionResult DeleteBillSession(int ID)
{
List<PatientViewModel> lst = (List<PatientViewModel>)Session["templist"];
lst.Remove(lst.FirstOrDefault(t => t.Count == ID));
Session["templist"] = lst;
return Json(new { success = "Valid" }, JsonRequestBehavior.AllowGet);
}
问题是它正在从会话中删除该行,但它正在删除序列中的最后一个,而不是我选择要删除的那个。
【问题讨论】:
标签: asp.net-mvc session model-view-controller asp.net-ajax