我正在使用 ASP.NET MVC4 和实体框架开发一个 Intranet Web 应用程序。我有一个视图,它列举了我在数据库中拥有的所有“人”,我可以编辑或删除它们。我正在尝试使用模态表单来确认删除操作(因此要使用我的操作“删除”)。但是,为了做到这一点,我必须检索我要删除的人的 ID。
阅读了示例和文档后,我找不到正确的方法来检索我想要删除的人的 id 以使用它来执行我的操作。
我的删除操作:
public ActionResult Delete(long id)
{
Person person = db.Persons.Single(p => p.Id_Person == id);
if (person == null)
{
return HttpNotFound();
}
db.Persons.DeleteObject(person);
db.SaveChanges();
return RedirectToAction("Index");
}
我的观点(和模态):
@model IEnumerable<BuSIMaterial.Models.Person>
@{
ViewBag.Title = "Index";
}
@using PagedList.Mvc;
@using PagedList;
<link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />
<h2>Index</h2>
<br />
<div class="group">
<div class ="btn-group">
<input type="button" value="New person" class="btn" onclick="location.href='@Url.Action("Create")';return false;"/>
<input type="button" value="Download report" class="btn" onclick="location.href='@Url.Action("PersonReport")';return false;"/>
</div>
</div>
@using (Html.BeginForm("SelectedPersonDetails", "Person"))
{
<form class="form-search">
<label for="person">Search an employee : </label>
<input type="text" id="tbPerson" name="tbPerson" class="input-medium search-query">
<button type="submit" class="btn">Search</button>
</form>
}
<br />
@Html.PagedListPager((IPagedList)ViewBag.PageOfPersons, page => Url.Action("Index", new { page }))
<br />
<table class="table table-striped">
<thead>
<tr>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
National Number
</th>
<th>
Start Date
</th>
<th>
End Date
</th>
<th>
Actions
</th>
</tr>
</thead>
<tbody>
@foreach (BuSIMaterial.Models.Person item in ViewBag.PageOfPersons)
{
<tr>
<td>
@item.FirstName
</td>
<td>
@item.LastName
</td>
<td>
@item.NumNat
</td>
<td>
@item.StartDate.ToShortDateString()
</td>
<td>
@if (item.EndDate.HasValue)
{
@item.EndDate.Value.ToShortDateString()
}
</td>
<td>
<div class="btn-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
Actions
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
@{
<li>@Html.ActionLink("Edit", "Edit", new { id = item.Id_Person })</li>
<li>@Html.ActionLink("Details", "Details", new { id = item.Id_Person })</li>
<li>@Html.ActionLink("Desactivate", "Desactivate", new { id = item.Id_Person })</li>
<li><a href="#myModal" data-toggle="modal" data-id="@item.Id_Person">Delete</a></li>
}
</ul>
</div>
</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<th>
First name
</th>
<th>
Last name
</th>
<th>
National number
</th>
<th>
Start date
</th>
<th>
End date
</th>
<th>
Actions
</th>
</tr>
</tfoot>
</table>
<div>@Html.PagedListPager((IPagedList)ViewBag.PageOfPersons, page => Url.Action("Index", new { page }))</div>
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Deleting an employee</h3>
</div>
<div class="modal-body">
<p>Are you sure you want to delete this person? All the concerned data also will be deleted.</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Delete</button>
</div>
</div>
@section Scripts
{
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/themes/base/css")
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$('#tbPerson').autocomplete({
source: '@Url.Action("AutoComplete")'
});
})
$('#myModal').modal(options)
</script>
}
我可以通过做得到 id,data-id ="@item.Id_Person"
但我不知道如何使用它来最终调用我的操作。有什么想法吗?