我有一个 DIV ( tenant-reference-photos
) ,其中包含显示照片的部分视图。每张照片旁边都有一个删除按钮。单击它时,我希望将照片从列表中删除,并且只有 DIV 由 Ajax 更新。
我正在使用Ajax.ActionLink
删除操作:
<div>
@Ajax.ActionLink("Delete", "DeleteReference",
new { id = photo.ImageId },
new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "POST",
UpdateTargetId = "tenant-reference-photos" },
new { @class = "btn btn-primary" })
</div>
控制器动作:
[HttpGet]
public ActionResult DeleteReference(int id)
{
return View(refPhotoRepository.Find(id));
}
[HttpPost, ActionName("DeleteReference")]
public ActionResult DeleteReferenceConfirmed(int id)
{
try
{
refPhotoRepository.Delete(id);
refPhotoRepository.Save();
return PartialView("_TenantReferencePhotosPartial");
}
catch (Exception e)
{
// handle exception
}
return View();
}
当我单击删除时,该操作将触发并从数据库中删除记录。问题出在return PartialView("_TenantReferencePhotosPartial");
. 当操作尝试返回部分时,我在@if (Model.ReferencePhotos.Count == 0)
.
_TenantReferencePhotosPartials.cshtml
<div>
@if (Model.ReferencePhotos.Count == 0)
{
<h3>You haven't uploaded any references!
@Ajax.ActionLink("Upload now?",
"TenantUploadReference",
new AjaxOptions
{
UpdateTargetId = "tenant-reference-photos",
InsertionMode = InsertionMode.Replace,
HttpMethod = "GET",
LoadingElementId = "ajax-loader"
})</h3>
}
else
{
foreach (var photo in Model.ReferencePhotos)
{
<ul class="thumbnails">
<li class="span8">
<a href="#" class="thumbnail">
<img src="@Url.Action("GetReference", "Tenants", new { id = photo.ImageId })" alt="@photo.Name") />
</a>
</li>
<li class="span4 btn-group btn-group-vertical">
<a href="#" class="btn btn-primary" id="showEditPhotoModal" role="button" data-toggle="modal">Edit</a>
@Ajax.ActionLink("Delete", "DeleteReference",
new { id = photo.ImageId },
new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "POST", UpdateTargetId = "tenant-reference-photos" },
new { @class = "btn btn-primary" })
</li>
</ul>
}
}
</div>
即使集合中有几张照片并删除了一张,仍然会在前面提到的行处抛出异常。一些帮助将不胜感激。