C# MVC4-删除Ajax.ActionLink意外动作的对象后刷新部分视图
我有一个DIV(C# MVC4-删除Ajax.ActionLink意外动作的对象后刷新部分视图,c#,asp.net-mvc,jquery,asp.net-mvc-4,C#,Asp.net Mvc,Jquery,Asp.net Mvc 4,我有一个DIV(租户参考照片),其中包含显示照片的局部视图。每张照片旁边都有一个删除按钮。单击它时,我希望照片从列表中删除,并且Ajax只更新DIV 我正在使用Ajax.ActionLink执行删除操作: <div> @Ajax.ActionLink("Delete", "DeleteReference", new { id = photo.ImageId }, new AjaxOptions { InsertionMode = InsertionMode.R
租户参考照片
),其中包含显示照片的局部视图。每张照片旁边都有一个删除按钮。单击它时,我希望照片从列表中删除,并且Ajax只更新DIV
我正在使用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>
单击delete(删除)时,将触发操作并从数据库中删除记录。问题在于返回PartialView(“\u tenantereferencephotosparial”)代码>。当操作尝试返回部分时,我在@if(Model.ReferencePhotos.Count==0)
处得到一个NullReferenceException
\u 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>
@if(Model.ReferencePhotos.Count==0)
{
你还没有上传任何参考资料!
@ActionLink(“立即上传?”,
“TenantUploadReference”,
新选择
{
UpdateTargetId=“租户参考照片”,
InsertionMode=InsertionMode.Replace,
HttpMethod=“GET”,
LoadingElementId=“ajax加载器”
})
}
其他的
{
foreach(模型中的var照片。参考照片)
{
-
-
@ActionLink(“删除”、“删除引用”,
新的{id=photo.ImageId},
新的AjaxOptions{InsertionMode=InsertionMode.Replace,HttpMethod=“POST”,UpdateTargetId=“租户参考照片”},
新的{@class=“btn btn primary”})
}
}
即使集合中有多张照片,其中一张被删除,异常仍会在前面提到的行中抛出。我们将非常感谢您的帮助
如何解决上述错误
我遇到了一些问题,jQuery$甚至没有在partials中定义
虽然它在源头上
我相信这与你的问题有关。当我在局部视图中遇到这种情况时,对我来说最有效的方法是将其包含在脚本部分
中。因此,在您编写脚本的\u Layout.cshtml
中,您可以添加以下内容:
// Do this after you referenced jquery
@RenderSection("scripts", required: false)
<script>
// some script references
</script>
更新:
你更新的问题打乱了我原来的答案。但让我先把最新的答案放在这里(然后再清理)。因此,出现空引用错误的原因是您没有返回模型:
refPhotoRepository.Delete(id);
refPhotoRepository.Save();
// build the model here and pass it to the partial view
// this will most probably involve querying your data store again
var model = goBuildTheModel();
return PartialView("_TenantReferencePhotosPartial", model;
你在哪里定义这个功能的?在同一.cshtml页面或在_layout.cshtml页面中。看起来它无法在您引用它的页面上解析。我在名为site.js的文件中定义了它,当我查看源代码时,它就在那里。我遇到了一些问题,jQuery$没有在partials中定义,即使它在源代码中。如果我解决了未定义的错误,那么我的js函数是否在刷新DIV的正确轨道上?您是否在head部分或底部看到站点和jQuery javascript文件的源代码,或者只看到指向它们的链接。一旦您看到这些文件的来源,您将发现这两个错误都已解决。现在,您正在使用-window.location.reload(true)刷新整个页面;要在不刷新页面的情况下更新div,只需将传入的html分配给div like-(“#div_id”).html(传入的_html);如果返回局部视图“及其相应的模型”,该怎么办?谢谢您的回复。我应该在我的问题中提到,可以从列表中显示许多照片,并且在每个照片旁边都有一个删除按钮(Ajax.ActionLink,根据问题),该按钮将photo.ImageId作为参数传递给控制器操作。出于这个原因,我想刷新DIV,这样剩余的照片仍然可见。那么,我是否需要根据@Typest的注释将传入的html分配给DIV?以RESTful的方式执行删除,您不应该返回html。状态(200 OK)即可。此外,在执行删除操作时,您没有“获取”资源,那么为什么要刷新整个div呢?当您应该只刷新您删除的“行”时。这是你做的更好的方法。哦,如果您的目的是确保您的用户拥有最新的视图,那么请执行重定向或另一个GET,然后从该GET刷新您的div。在我显示照片的部分中,我首先检查(list.count==0),是否向用户显示了一个Ajax.ActionLink,以便上传图像。我尝试RESTful方法的原因是,在成功删除照片后,当我重定向到返回照片部分的操作时,我在if(list.count==0)处得到一个NullReferenceException。如果列表中有多张照片,我单击其中一张照片上的“删除”,甚至会发生这种情况。所有这一切都有点让人困惑…但是基于RESTful方法的删除不应该返回视图或html。我已经用我最初想做的事情更新了我的问题。如果你能再看一眼,我将不胜感激。
@section scripts {
$(function() {
<script>
function onDeleteReferenceSuccess(data, status, xhr) {
if (data.error) { /* handle error */ }
else {
window.location.reload(true);
}
}
</script>
});
}
else {
// instead of this -> window.location.reload(true);
$("#id_of_the_div").remove();
}
refPhotoRepository.Delete(id);
refPhotoRepository.Save();
// build the model here and pass it to the partial view
// this will most probably involve querying your data store again
var model = goBuildTheModel();
return PartialView("_TenantReferencePhotosPartial", model;