【发布时间】:2016-09-05 10:53:37
【问题描述】:
这个问题是我在以下链接中发布的上一个问题的延续:
总结:我正在尝试删除一个文件。由于上述帖子中的答案,我现在拥有所有必要的功能。但是,偶尔,如果文件正在使用中(例如,如果我在删除之前下载了文件,就会发生这种情况),我会收到 IOException。
我想处理这个异常并在发生这种情况时向用户显示一条消息。
我尝试调试 jQuery 代码,但每当我在其上设置断点时,我就会开始跳过 1000 行 jQuery 库。因此,作为一种快速的替代方案,我只是在各处放置警报。
我发现大部分代码都没有被执行 - 所以我把 cmets 放在我认为我应该看到消息但没有看到的地方。因此,我无法调试或弄清楚代码应该做什么。
所以我的第一个问题是如何让异常显示。似乎即使这是 Ajax,整个页面仍然会刷新,这对我来说不是 Ajax 调用的预期行为(因此,如果显示错误,它可能会在页面刷新时丢失)。但是,对于所有警报,我应该在某处看到错误,但我没有。我有目的地改变了工作代码,现在总是抛出异常。同样,删除功能有效,但错误报告失败。
我的下一个问题是简要解释为什么需要代码的每个部分(突出显示),因为我不知道为什么这些部分存在,我无法弄清楚,因为我无法调试或显示警报他们。
下面是索引视图和相关代码:
@model IEnumerable<FileInfo>
@{
ViewBag.Title = "File List";
}
<h2>Index</h2>
<p>@Html.ActionLink("Upload", "Upload")</p>
<p>@Html.ValidationSummary(true, "", new { @class = "text-danger" })</p>
<p>@Html.ValidationMessage("Name", new { @class = "text-danger" })</p>
<span class="message text-danger"></span>
<table class="table">
<tr>
<th>File Name</th>
<th>Actions</th>
</tr>
@foreach (FileInfo file in Model)
{
<tr>
<td>@file.Name</td>
<td>
<form class="deleteform">
@Html.AntiForgeryToken()
<input type="hidden" name="fileName" value="@file.Name" />
<input type="submit" value="delete" />
</form>
</td>
<td>@Html.ActionLink("Download", "Download", new { fileName = file.Name })</td>
</tr>
}
</table>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(document).ready()
{
var url = '@Url.Action("Delete", "FileManagement")';
$('.deleteform').submit(function ()
{
alert(".deleteform.submit entered...");//This alert shows
return confirm("are you sure ...");//This alert shows
var formData = $(this).serialize();
alert(formData);//This does NOT show.
var row = $(this).closest('tr');
alert(row);//This does NOT show.
$.post(url, formData, function (response)
{
alert("$.post() entered...");//This does NOT show.
alert(response);//This does NOT show.
if (response)
{
alert("response true");//This does NOT show.
alert(response);//This does NOT show.
//row.remove(); //This code actually works even though the alert above does not show.
} else
{
alert("response false");//This does NOT show. - I dont know what this section of code is for.
//alert("Error 1 - display message");
// Oops - display message?
}
alert("$.post() finished...");//This does NOT show.
}).fail(function (response)
{
alert("$.fail() entered...");//This does NOT show. - I dont know what this section of code is for.
alert("Error 2 - display another message");//This does NOT show.
// Oops
alert("$.fail() finished...");//This does NOT show.
});
return false; // cancel the default submit
alert(".deleteform.submit finished...");//This does NOT show.
});
}
</script>
}
下面是这个视图的控制器和删除操作:
public ActionResult Index()
{
DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/UserFiles"));
List<FileInfo> files = dirInfo.GetFiles().ToList();
return View(files);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(string fileName)
{
var path = Path.Combine(Server.MapPath("~/UserFiles"), fileName);
if (System.IO.File.Exists(path))
{
try
{
//System.IO.File.Delete(path);
throw new IOException("Hello - Test Message...");
}
catch (IOException e)
{
Response.StatusCode = 500;
Response.StatusDescription = e.Message;
//ModelState.AddModelError("Name", e.Message);
}
}
else
return Json(null);
//return HttpNotFound();
return Json(true);
//return RedirectToAction("Index");
}
我将不胜感激。
【问题讨论】:
-
您不能使用
return confirm("are you sure ...")- 退出该方法(如果您正在处理默认提交,您只会使用它,但在您的情况下,您取消它并改为进行ajax调用)。而是使用if (confirm("....") { // make you ajax call } -
我弄清楚了当状态码为 500 时会发生失败,以及如何将消息发送到前端(这样我以后才能弄清楚如何显示它)。我还想出了 if(response) = true 部分,那是一切顺利的时候。我看不到的唯一部分是 if(response)...else 部分。我认为除了 200 之外的任何东西都会将我们发送到 .fail 部分。那么为什么我们需要 else 部分,实际上,没有 else,为什么还要 if(response) 呢?再次感谢您的帮助。
-
如果您想返回错误的详细信息,则不需要它(我在上一个问题中给出的代码假设如果无法删除文件,您将使用
return Json(null);(并且在if(response)块中,您只会显示一个“无法删除文件” 一般错误 - 公开错误的具体细节通常不好 - 它只会为恶意用户提供有关潜在漏洞的额外信息在您的应用中) -
@StephenMuecke - 所以...感谢您按照 if(confirm) 的建议更改 return(confirm) 行。我设法找出其余问题并显示错误消息。如果您想在有机会时将其作为答案,我会接受。一如既往,斯蒂芬,再次感谢您的所有帮助和建议。
标签: c# jquery ajax asp.net-mvc