1

我的 mvc 网站上有反馈表,我将此表发送到电子邮件。
如果电子邮件发送失败,我想显示错误消息,如果电子邮件发送成功,我想显示成功消息。我尝试通过 ViewBag 做到这一点。
我在我的控制器中添加

    [HttpGet]
    public ActionResult Feedback(string Message)
    {
        if (Message != null)
        {
            if (Message == "No")
            {
                ViewBag.Message = "Error";
            }

            if (Message == "Yes")
            {
                ViewBag.Message = "Success";
            }
        }

        else
        {
            ViewBag.Message = null;
        }

        return View();
    }

    [HttpPost]
    public ActionResult Feedback(FeedbackForm Model)
    {
        string Message;

        //email
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.BodyEncoding = Encoding.UTF8;
        msg.Priority = MailPriority.High;

        msg.From = new MailAddress(Model.Email, Model.Name);
        msg.To.Add(/*"evaluation@corepartners.ru"*/"tayna-anita@mail.ru");

        msg.Subject = @Resources.Global.Feedback_Email_Title + " " + Model.Company;
        string message = @Resources.Global.Feedback_Email_From + ": " + Model.Name + "\n"
                        + @Resources.Global.Feedback_Email + ": " + Model.Email + "\n"
                        + @Resources.Global.Feedback_Phone + ": " + Model.Phone + "\n"
                        + @Resources.Global.Feedback_Company + ": " +  Model.Company + "\n\n"
                        + Model.AdditionalInformation;
        msg.Body = message;
        msg.IsBodyHtml = false;

        //Attachment
        if (Model.ProjectInformation != null && !(String.IsNullOrEmpty(Model.ProjectInformation.FileName)))
        {
            HttpPostedFileBase attFile = Model.ProjectInformation;
            if (attFile.ContentLength > 0)
            {
                var attach = new Attachment(attFile.InputStream, attFile.FileName);
                msg.Attachments.Add(attach);
            }
        }

        SmtpClient client = new SmtpClient("denver.corepartners.local", 55);
        client.UseDefaultCredentials = false;
        client.EnableSsl = false;

        try
        {
            client.Send(msg);
            return RedirectToAction("Feedback", "Home", Message = "Yes");
        }

        catch (Exception ex)
        {
            return RedirectToAction("Feedback", "Home", Message = "No");
        }
    }

我在我的视图中添加了

    @if (ViewBag.Message != null)
    {
        <p style="color: red;">@ViewBag.Message</p>
    }

但无论如何我都没有收到任何消息。
怎么了?

4

1 回答 1

3

尝试这个:

return RedirectToAction("Feedback", "Home", new { Message = "Yes" });
于 2013-06-03T12:19:02.417 回答