从特定索引处的会话中删除 Entity Framework
Delete from session at specific Index Entity Framework
我有一个添加账单详细信息的表格,如屏幕截图所示:
在此表单中,首先通过以下代码添加存储在会话中的条目:
[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);
}
问题是它正在从会话中删除行,但它删除的是序列中的最后一行,而不是我选择删除的行。
如果有人遇到同样的情况,问题是这样解决的:
查看:
<table id="tblBills" class="table table-bordered table-striped table-hover js-basic-example dataTable">
<tr>
<th style="width:100px">Patient Appointmnet ID</th>
<th style="width:100px">Amount</th>
<th style="width:150px">Description</th>
<th style="width:40px">Action</th>
</tr>
@{
double? AmountTotal = 0;
if (Session["templist"] != null)
{
List<PatientViewModel> lst = (List<PatientViewModel>)Session["templist"];
int count = 0;
foreach (var o in lst)
{
<tr>
<td class="Count" style="display:none">
<span>@count</span>
</td>
<td class="AppointmentID">
<span>@o.PatientAppointmentID</span>
</td>
<td class="Amount">
<span>@o.Amount</span>
</td>
<td class="Description">
<span>@o.Description</span>
</td>
<td>
<a class="Delete" href="javascript:;">Delete</a>
</td>
</tr>
AmountTotal += @o.Amount;
count++;
}}
else
{
<tr>
<td colspan="4"> No Data Found</td>
</tr>
}
}
</table>
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();
$.ajax({
type: "POST",
url: "/Session_Delete",
data: '{ID: ' + id+ '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("#tblBills").load(window.location + " #tblBills");
}
});
}
});
控制器方法:
[HttpPost]
public ActionResult DeleteBillSession(int ID)
{
List<PatientViewModel> lst = (List<PatientViewModel>)Session["templist"];
lst.RemoveAt(ID);
Session["templist"] = lst;
// return new EmptyResult();
return Json(new { success = "Valid" }, JsonRequestBehavior.AllowGet);
}
我有一个添加账单详细信息的表格,如屏幕截图所示:
在此表单中,首先通过以下代码添加存储在会话中的条目:
[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);
}
问题是它正在从会话中删除行,但它删除的是序列中的最后一行,而不是我选择删除的行。
如果有人遇到同样的情况,问题是这样解决的:
查看:
<table id="tblBills" class="table table-bordered table-striped table-hover js-basic-example dataTable">
<tr>
<th style="width:100px">Patient Appointmnet ID</th>
<th style="width:100px">Amount</th>
<th style="width:150px">Description</th>
<th style="width:40px">Action</th>
</tr>
@{
double? AmountTotal = 0;
if (Session["templist"] != null)
{
List<PatientViewModel> lst = (List<PatientViewModel>)Session["templist"];
int count = 0;
foreach (var o in lst)
{
<tr>
<td class="Count" style="display:none">
<span>@count</span>
</td>
<td class="AppointmentID">
<span>@o.PatientAppointmentID</span>
</td>
<td class="Amount">
<span>@o.Amount</span>
</td>
<td class="Description">
<span>@o.Description</span>
</td>
<td>
<a class="Delete" href="javascript:;">Delete</a>
</td>
</tr>
AmountTotal += @o.Amount;
count++;
}}
else
{
<tr>
<td colspan="4"> No Data Found</td>
</tr>
}
}
</table>
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();
$.ajax({
type: "POST",
url: "/Session_Delete",
data: '{ID: ' + id+ '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("#tblBills").load(window.location + " #tblBills");
}
});
}
});
控制器方法:
[HttpPost]
public ActionResult DeleteBillSession(int ID)
{
List<PatientViewModel> lst = (List<PatientViewModel>)Session["templist"];
lst.RemoveAt(ID);
Session["templist"] = lst;
// return new EmptyResult();
return Json(new { success = "Valid" }, JsonRequestBehavior.AllowGet);
}