我是MVC和JQuery的初学者。我正在表中显示数据库中的记录。现在,删除其中一条记录后,表格不再显示记录了。这就是我要做的事情。这一点。
我在控制器中有2个动作方法。 指数行动方法
public ActionResult Index(int? page)
{
Provider providerList = new Provider();
IList<Provider> providers = DAL.GetListofProviders.ToList();
int pageSize = 5;
int pageNumber = (page ?? 1);
return View(providers.ToPagedList(pageNumber,pageSize));
}
删除操作方法
public ActionResult Delete(int id)
{
DAL.DeleteProvider(id);
return RedirectToAction("Index");
}
我的观点如图所示
@using PagedList;
@using PagedList.Mvc;
@model IPagedList<DEMO_JAN14.Models.Provider>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<head>
<title>LIST OF PROVIDERS</title>
</head>
<body>
<div class="container">
<h1>Providers List</h1><br/>
<a class="btn btn-primary col-sm-1" href="/Provider/Create">Create</a><br/>
<table class="table table-bordered" id="tblData">
<tr>
<th>Provider Type</th>
<th>First Name</th>
<th>Last Name</th>
<th>Certification</th>
<th>Specialization</th>
<th>SSN</th>
<th>Facility Name</th>
<th>Contact No</th>
<th>Contact Email</th>
</tr>
<tbody data-bind="foreach:viewModel.DataList">
<tr>
<td class="col-lg-2" data-bind="text: ProviderType"></td>
<td class="col-lg-2" data-bind="text: FirstName"></td>
<td class="col-lg-2" data-bind="text: LastName"></td>
<td class="col-lg-2" data-bind="text: Certification"></td>
<td class="col-lg-2" data-bind="text: Specialization"></td>
<td class="col-lg-2" data-bind="text: SSN"></td>
<td class="col-lg-2" data-bind="text: FacilityName"></td>
<td class="col-lg-2" data-bind="text: ContactNumber"></td>
<td class="col-lg-2" data-bind="text: ContactEmail"></td>
<td><button type="button" id="Edit" class="btn btn-primary" data-bind="click:$root.Edit">Edit</button></td>
<td><button type="button" id="Delete" class="btn btn-primary" data-bind="click:$root.Delete">Delete</button></td>
</tr>
</tbody>
</table>
Page @(Model.PageCount<Model.PageNumber? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager( Model, page => Url.Action("Index", new { page }) )
</div>
</body>
<script type="text/javascript" src="../../Scripts/knockout.mapping- latest.js"></script>
<script type="text/javascript" src="~/Scripts/Index.js"></script>
<script type="text/javascript">
var data = @Html.Raw(Json.Encode(Model));
</script>
JS for delete
function ProviderViewModel() {
var self = this;
self.ProviderID = ko.observable("");
self.ProviderType = ko.observable("");
self.FirstName = ko.observable("");
self.LastName = ko.observable("");
self.Certification = ko.observable("");
self.Specialization = ko.observable("");
self.SSN = ko.observable("");
self.ContactNumber = ko.observable("");
self.ContactEmail = ko.observable("");
self.FacilityName = ko.observable("");
self.DataList = ko.observableArray([]);
var Provider =
{
ProviderID: self.ProviderID,
ProviderType: self.ProviderType,
FirstName: self.FirstName,
LastName: self.LastName,
Certification: self.Certification,
Specialization: self.Specialization,
SSN: self.SSN,
ContactNumber: self.ContactNumber,
ContactEmail: self.ContactEmail,
FacilityName: self.FacilityName,
};
self.Provider = ko.observable();
self.Providers = ko.observableArray();
// Delete Provider
self.Delete = function (Provider) {
if (confirm('Are you sure you want to delete this row?')) {
var id = Provider.ProviderID();
$.ajax({
url: "/Provider/Delete/" + Provider.ProviderID(),
cache: false,
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: ko.toJSON(id),
success: function (data) {
self.Providers.remove(Provider);
// alert("Record Deleted Successfully");
}
}).fail(
function (xhr, textStatus, err) {
alert(err);
});
}
}
记录被删除了,但只有当我刷新页面时才会看到删除。换句话说,删除操作方法中的重定向到索引没有发生。有人请指出我正确的方向。