Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“显示消息”;电子邮件发送失败/成功“;asp.NETMVC4_Asp.net_Asp.net Mvc 4 - Fatal编程技术网

“显示消息”;电子邮件发送失败/成功“;asp.NETMVC4

“显示消息”;电子邮件发送失败/成功“;asp.NETMVC4,asp.net,asp.net-mvc-4,Asp.net,Asp.net Mvc 4,我在mvc网站上有反馈表,我将此表发送至电子邮件。 在我的控制器中,我在电子邮件发送失败的情况下创建了ErrorMessage,在电子邮件发送成功的情况下创建了SuccessMessage /*Feedback*/ [HttpGet] public ActionResult Feedback(string ErrorMessage) { if (ErrorMessage != null) { } return View(); } [HttpPost] publ

我在mvc网站上有反馈表,我将此表发送至电子邮件。
在我的控制器中,我在电子邮件发送失败的情况下创建了ErrorMessage,在电子邮件发送成功的情况下创建了SuccessMessage

/*Feedback*/
[HttpGet]
public ActionResult Feedback(string ErrorMessage)
{
    if (ErrorMessage != null)
    {
    }

    return View();
}

[HttpPost]
public ActionResult Feedback(FeedbackForm Model)
{
    string ErrorMessage, SuccessMessage;

    //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("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);
        SuccessMessage = "Email sending was successful"
    }

    catch (Exception ex)
    {
        return RedirectToAction("Feedback", "Home", ErrorMessage = "Email sending failed");
    }

    return RedirectToAction("Feedback", "Home");
}

如何添加并在我的视图中显示此消息?

当您重定向到新页面时,请使用TempData,它将在重定向后的下一个请求中可用。将消息放入TempData[“message”]并在反馈视图中输出。为了更好地检查

     <% TempData["Message"] != null { %>
     <%= TempData["Message"] %>;
     <%} %>

;

您不能尝试按如下方式访问这些模型属性:

<%= Model.ErrorMessage %>

<%= Model.SuccessMessage %>

使用

您可以使用TempDataDictionary对象传递数据,传递数据的方式与使用ViewDataDictionary对象的方式相同。但是,TempDataDictionary对象中的数据仅在一个请求到下一个请求之间保持,除非使用Keep方法将一个或多个键标记为保留。如果某个密钥被标记为保留,则该密钥将保留以供下次请求使用

TempDataDictionary对象的一个典型用途是,当它重定向到另一个操作方法时,从一个操作方法传递数据。例如,action方法在调用RedirectToAction方法之前,可能会在控制器的TempData属性(返回TempDataDictionary对象)中存储有关错误的信息。然后,下一个操作方法可以处理错误并呈现显示错误消息的视图


您也可以在模型的异常中设置viewdata(“Error”)=“Message Failure”,然后简单地将viewdata的值输出到视图中……您可以给我演示如何将此异常添加到我的模型中的示例吗?您可以给我演示如何将此异常添加到我的模型中的示例吗?
[HttpPost]
public ActionResult Feedback(FeedbackForm Model)
{
    bool error = true;
    
    if(error){
        TempData["Message"] = "Error";
        TempData["Error"] = true;            
    }
    else{
        TempData["Message"] = "Success";
        TempData["Error"] = false;            
    }

    return RedirectToAction("Feedback", "Home");
}

[HttpGet]
public ActionResult Feedback()
{
    string message = TempData["Message"].ToString();
    bool error = Convert.ToBoolean(TempData["Error"]);

    var model = new FeedbackModel{Message = message, Error = error};

    return View(model);
}