从特定索引实体框架的会话删除

问题描述 投票:0回答:1

我有一个添加帐单详细信息的表格,如屏幕截图所示:

enter image description here

此人首先通过此代码添加存储在会话中的条目:

[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
1个回答
0
投票
如果有人遇到相同的情况,问题将通过以下方式解决:

查看:

<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); }
© www.soinside.com 2019 - 2024. All rights reserved.