删除后显示成功信息

发布于 2024-12-29 08:04:26 字数 978 浏览 1 评论 0原文

我需要在删除文件后向用户显示成功消息。不知道该怎么做。请帮忙。

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感性不性感 2025-01-05 08:04:26

//我应该在这里返回什么来显示消息?

ViewBag.SuccessMessage = "File was successfully deleted";
return View();

在删除生成发票视图中,将其写入有意义的位置:

<p>@(ViewBag.SuccessMessage ?? "")</p>

//What shall i return here to display message?

ViewBag.SuccessMessage = "File was successfully deleted";
return View();

In your deleteGeneratedInvoice view, write this somewhere that makes sense:

<p>@(ViewBag.SuccessMessage ?? "")</p>
維他命╮ 2025-01-05 08:04:26

好吧,如果您要使用此操作删除应用程序中的数据,则应该将其以表单形式发布到服务器,并指定对操作的限制,使其仅响应 HttpPost,否则一旦有东西尝试抓取您的应用程序,您就会发现将会猛然惊醒:-P

话虽如此,对于返回的信息,您有三种选择。您可以返回一个新页面(如果它是一个空白页面,除了“文件已成功删除”消息之外,这有点愚蠢)。

或者,您可以通过使用 yAjax.BeginForm() 而不是 Html.BeginForm() 定义表单,将表单设置为使用 Ajax 进行回发,后者为您提供了另外两个选项。要么从删除操作返回部分视图,并在响应完成时在动态添加的 DIV 中显示部分视图(在我看来最灵活),或者您可以从删除操作返回一个简单的返回代码,然后根据该情况返回代码在您的页面上显示不同的消息。可以使用 BeginForm() 方法的 AjaxOptions 参数来定义处理这些响应的 Javascript 方法。它们是使用 OnSuccessOnErrorOnComplete 属性指定的 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 yAjax.BeginForm() insead of Html.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 the AjaxOptions parameter of the BeginForm() method. They are the javascript methods specified using the OnSuccess, OnError and OnComplete 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文