删除后显示成功信息
我需要在删除文件后向用户显示成功消息。不知道该怎么做。请帮忙。
public ActionResult deleteGeneratedInvoice(string invoiceNumber)
{
try
{
string fileName = invoiceNumber.Trim() + ".pdf";
string filePath = HostingEnvironment.MapPath("~/Content/reports/");
string fullFilePath = filePath + fileName;
System.IO.File.Delete(fullFilePath);
//What shall i return here to display message?
return
}
catch (Exception e)
{
InvoiceSearchTool.Models.udtExceptionTable exception = new udtExceptionTable();
exception.MethodName = "deleteGeneratedInvoice";
exception.Exception = e.ToString();
exception.Date = DateTime.Now;
DYNAMICS_EXTEntities db = new DYNAMICS_EXTEntities();
db.AddToudtExceptionTables(exception);
db.SaveChanges();
//return View ("Error");
}
}
I need to display a success message to user after the file has been deleted. dont know how to do it. please help.
public ActionResult deleteGeneratedInvoice(string invoiceNumber)
{
try
{
string fileName = invoiceNumber.Trim() + ".pdf";
string filePath = HostingEnvironment.MapPath("~/Content/reports/");
string fullFilePath = filePath + fileName;
System.IO.File.Delete(fullFilePath);
//What shall i return here to display message?
return
}
catch (Exception e)
{
InvoiceSearchTool.Models.udtExceptionTable exception = new udtExceptionTable();
exception.MethodName = "deleteGeneratedInvoice";
exception.Exception = e.ToString();
exception.Date = DateTime.Now;
DYNAMICS_EXTEntities db = new DYNAMICS_EXTEntities();
db.AddToudtExceptionTables(exception);
db.SaveChanges();
//return View ("Error");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
//我应该在这里返回什么来显示消息?
在删除生成发票视图中,将其写入有意义的位置:
//What shall i return here to display message?
In your deleteGeneratedInvoice view, write this somewhere that makes sense:
好吧,如果您要使用此操作删除应用程序中的数据,则应该将其以表单形式发布到服务器,并指定对操作的限制,使其仅响应 HttpPost,否则一旦有东西尝试抓取您的应用程序,您就会发现将会猛然惊醒:-P
话虽如此,对于返回的信息,您有三种选择。您可以返回一个新页面(如果它是一个空白页面,除了“文件已成功删除”消息之外,这有点愚蠢)。
或者,您可以通过使用 y
Ajax.BeginForm()
而不是Html.BeginForm()
定义表单,将表单设置为使用 Ajax 进行回发,后者为您提供了另外两个选项。要么从删除操作返回部分视图,并在响应完成时在动态添加的 DIV 中显示部分视图(在我看来最灵活),或者您可以从删除操作返回一个简单的返回代码,然后根据该情况返回代码在您的页面上显示不同的消息。可以使用BeginForm()
方法的AjaxOptions
参数来定义处理这些响应的 Javascript 方法。它们是使用OnSuccess
、OnError
和OnComplete
属性指定的 javascript 方法。请注意,它们不是必需的,但为了获得最佳用户体验,您应该确保至少为成功和错误指定方法。Well if you're deleting data in your app using this action, you should have it posted to the server in a form and specify a restriction on your action that it only responds to HttpPost, otherwise as soon as something tries to crawl your app you're going to be in for a rude awakening :-P
That being said, you have three choices for the returned information. You can return a new page (which is kind of silly if it's a blank page except for the "File deleted successfully" message).
Or you can setup your form to postback using Ajax by defining your form with y
Ajax.BeginForm()
insead ofHtml.BeginForm()
which gives you the two other options. Either return a Partial View from your delete action and have the partial view shows in a dynamically added DIV when the response completes (the most flexible in my eyes), or you can return a simple return code from your Delete action and then depending on that return code show different messages on your page. The Javascript methods that would handle these responses can be defined using theAjaxOptions
parameter of theBeginForm()
method. They are the javascript methods specified using theOnSuccess
,OnError
andOnComplete
properties. Note they are not required but for the best user experience you should make sure to at least specify methods for the Success and Error ones.