2013-06-03 34 views
0

我在我的mvc网站上有反馈表单,我将此表格发送至电子邮件。
在我控制我的情况下,电子邮件创建的ErrorMessage发送失败和SuccessMessage的情况下,邮件发送成功显示消息“电子邮件发送失败/成功”asp.net mvc 4

/*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("[email protected]"); 

    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"); 
} 

如何添加展示在我看来,这个消息?

+0

你也可以设置你的模型的异常内的可视数据(“错误”)=“消息失败”&然后可视数据以查看简单地输出的值.... <%=可视数据(“错误” )%> – highwingers

+0

你能告诉我如何将这个异常添加到我的模型吗? – Heidel

回答

0

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

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

你不能试图访问这些作为模型属性如下:

<%= Model.ErrorMessage %> 

<%= Model.SuccessMessage %> 
+0

你能告诉我示例如何将这个异常添加到我的模型? – Heidel

0

使用TempData

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

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

[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); 
}