我想在索引页中删除表行,并且在删除行之前,我必须在数据库中进行修改并显示弹出窗口以确认删除操作。但是,当我删除该行时,它已在数据库中删除,但是在Ajax中执行成功操作后,我的页面没有刷新。下面是我为视图和控制器编写的代码,并给出了模型定义作为参考。
// Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using LAMS_Master.Models;
namespace LAMS_Master.Controllers
{
public class Acquisition_Lease_Case_RecordController : Controller
{
private ApplicationDbContext db;
public Acquisition_Lease_Case_RecordController()
{
db = new ApplicationDbContext();
}
protected override void Dispose(bool disposing)
{
db.Dispose();
}
// GET: Acquisition_Lease_Case_Record
public ActionResult Index()
{
List<acquisition_lease_case_record_master> listdata = new List<acquisition_lease_case_record_master>();
foreach(var d in db.acquisition_lease_case_record_master.Where(c => c.int_deleted_sts == 0).Include(c => c.vill_code).ToList())
{
d.chrv_land_class_cd = db.landclasslistmaster.Where(c => c.chrv_landclass_cd == d.chrv_land_class_cd).Select(c => c.chrv_landclass).SingleOrDefault();
listdata.Add(d);
}
return View("Index",listdata);
}
public ActionResult Create()
{
ViewBag.village = new SelectList(db.village_master.Where(c => c.int_deleted_sts == 0).ToList(), "chrv_vill_code", "chrv_vill_name");
ViewBag.landclass = new SelectList(db.landclasslistmaster.ToList(), "chrv_landclass_cd", "chrv_landclass");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(acquisition_lease_case_record_master alcrm)
{
if (ModelState.IsValid)
{
alcrm.fk_chrv_proj_code = "LAMS111811PCB06";
alcrm.int_created_by = 0;
alcrm.int_deleted_sts = 0;
alcrm.chrv_ip_address = UilityMaster.GetLocalIPAddress();
db.acquisition_lease_case_record_master.Add(alcrm);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.village = new SelectList(db.village_master.Where(c => c.int_deleted_sts == 0).ToList(), "chrv_vill_code", "chrv_vill_name");
ViewBag.landclass = new SelectList(db.landclasslistmaster.ToList(), "chrv_landclass_cd", "chrv_landclass");
return View();
}
[HttpGet]
public ActionResult Edit(string case_no)
{
var data = db.acquisition_lease_case_record_master.Include(c=>c.vill_code).Where(c=>c.chrv_case_no==case_no).SingleOrDefault();
data.chrv_land_class_cd = db.landclasslistmaster.Where(c => c.chrv_landclass_cd == data.chrv_land_class_cd).Select(c => c.chrv_landclass).SingleOrDefault();
return View("EditALCRM", data);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(acquisition_lease_case_record_master alcrmedit)
{
if (alcrmedit.num_case_area!=null && alcrmedit.tms_case_dt!=null && alcrmedit.chrv_case_no!=null)
{
var casefromdb = db.acquisition_lease_case_record_master.Find(alcrmedit.chrv_case_no);
casefromdb.num_case_area = alcrmedit.num_case_area;
casefromdb.tms_case_dt = alcrmedit.tms_case_dt;
db.Entry(casefromdb).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View("EditALCRM", alcrmedit);
}
public JsonResult DeleteCaseRecord(string case_no)
{
var deletedata = db.acquisition_lease_case_record_master.Find(case_no);
bool result = false;
if (deletedata != null)
{
deletedata.int_deleted_sts = 1;
db.Entry(deletedata).State = EntityState.Modified;
db.SaveChanges();
result = true;
}
return Json(result, JsonRequestBehavior.AllowGet);
}
}
}
//索引页
@model IEnumerable<LAMS_Master.Models.acquisition_lease_case_record_master>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="wrapper">
<div class="panel panel-default">
<h3 class="panel-heading">Case Details</h3>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Village</th>
<th>Land Class</th>
<th>Case No</th>
<th>Case Date</th>
<th>Case Area</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var v in Model)
{
<tr id="@v.chrv_case_no">
<td>
@v.vill_code.chrv_vill_name
</td>
<td>
@v.chrv_land_class_cd
</td>
<td>
@v.chrv_case_no
</td>
<td>
@v.tms_case_dt.ToString("dd-MMM-yyyy")
</td>
<td>
@v.num_case_area
</td>
<td>
@Html.ActionLink("Edit", "Edit", "Acquisition_Lease_Case_Record", new { case_no = v.chrv_case_no }, null) |
<a href="#" class="deleteLink" onclick="ConfirmDelete('@v.chrv_case_no')">Delete</a>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
<input type="hidden" id="hiddencaseno" value="" />
</div>
@*Modal Defination*@
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<a href="#" class="close" data-dismiss="modal">×</a>
<h3 class="modal-title">Delete</h3>
</div>
<div class="modal-body">
<h4>Are you sure to delete ?</h4>
<div style="text-align:center;display:none" id="loaderDiv">
<img src="~/Images/loader.gif" width="150" />
</div>
</div>
<div class="modal-footer">
<a href="#" class="btn btn-default" data-dismiss="modal">Cancel</a>
<a href="#" class="btn btn-success" onclick="DeleteItem()">Confirm</a>
</div>
</div>
</div>
</div>
//索引页脚本
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
var ConfirmDelete = function (actid) {
$("#hiddencaseno").val(actid);
$("#myModal").modal('show');}
var DeleteItem = function () {
$("#loaderDiv").show();
var actid = $("#hiddencaseno").val();
$.ajax({
type: 'POST',
url: '@Url.Action("DeleteCaseRecord")',
data: { case_no: actid },
success: function (result) {
$("#loaderDiv").hide();
$("#myModal").modal('hide');
alert('#' + actid);
$('#'+actid).remove(); }})}
</script>