首页 > 解决方案 > C# ASP.NET MVC 路由不返回成功页面的视图

问题描述

我有一个ActionResult方法,最后我希望它返回成功页面。它击中了返回 View("ExportFile") 但什么也没发生。

这是我的操作方法:

[HttpPost]
//[ValidateAntiForgeryToken]
public ActionResult ExportFile(string[] years, string[] months, string[] radio, string[] emails, string acctNum)
{
        // Is Account Number empty or Radio not selected
        if (String.IsNullOrEmpty(acctNum.Trim()) && radio == null)
        {
            return Json(new { success = false, message = "* Please Enter Account Number\n\n * Please choose whether to download or email statements" });
        }

        if (acctNum == "")
        {
            return Json(new { success = false, message = "* Please Enter Account Number" });
        }
        else if (radio == null)
        {
            return Json(new { success = false, message = "* Please Choose whether To Download or Email" });
        }

        try
        {
            // If only account number is entered
            if (years == null)
            {
                if (months == null)
                {
                    using (var db = new FMBDBPRDEntities1())
                    {
                        allPaths = db.ClientStatement_Inventory
                                               .Where(x => x.accountNum == acctNum)
                                               .Select(c => c.statementPath).ToList();
                    }

                    if (allPaths.Count == 0)
                    {
                        return Json(new { success = false, message = $"There were no documents for Account#: {acctNum}" });
                    }
                }
            }
            // If "Emailing Statements" is chosen
            else if (radio[0] == "Email Statements")
            {
                // Make array of emails into List for sending in email 
                if (emails.ToString() != "")
                {
                    var allEmails = emails[0].Split(',');

                    foreach (var email in allEmails)
                    {
                        if (emailValid.IsMatch(email))
                        {
                            everyEmail.Add(email);
                        }
                        else
                        {
                            return Json(new { success = false, message = $"* Not valid email address: {email}.\n\n * Please double check and try again." });
                        }
                    }

                    MemoryStream output = new MemoryStream();

                    List<string> distinctFiles = allPaths
                        .GroupBy(x => x.Split(new char[] { '\\' }).Last())
                        .Select(x => x.First())
                        .ToList();

                    using (ZipFile zip = new ZipFile())
                    {
                        zip.AddFiles(distinctFiles, @"\");
                        zip.Save(output);

                        output.Position = 0;
                        var attachmentResult = output.Length;

                        if (attachmentResult > 24500000)
                        {
                            return Json(new { success = false, message = "* Email attachment is too large.\n\n * Please select Download to download your files. " });
                        }
                        else
                        {
                            return View("ExportFile");

                            //DBQueries.SendEmail(everyEmail, output, fromAddress, "Client Statement Reports", "", true);
                        }

最后 "return View("ExportFile") 不会返回我的视图调用ExportFile。动作名称和返回视图需要是相同的名称对吗?

标签: c#asp.net-mvc

解决方案


推荐阅读