MVC帖子没有返回正确的ViewResult

时间:2016-08-03 21:21:03

标签: c# asp.net-mvc asp.net-mvc-viewmodel

在我网站的联系页面的POST端点中,该页面根据用户输入向网站所有者发送电子邮件,我将返回相同视图的ViewResult,但使用新初始化(空)视图模型。

我的客户目标是,在收到POST响应后,为用户提供相同的页面,但所有表单字段都已清空。然而,与此不同,用户最终在同一页面上仍然填写了所有相同的表单信息。电子邮件发送成功,不会引发任何错误。

有什么想法吗?

以下是我的GET和POST端点:

[HttpGet]
public ViewResult contact()
{
    return View(new ContactUsViewModel());
}

[HttpPost]
public async Task<ViewResult> contact(ContactUsViewModel inputModel)
{
    try
    {
        if (ModelState.IsValid)
        {
            string body = 
                "<div style='font-family: Arial, Helvetica, sans-serif; font-size: 13px; color: #444444;'>" +
                "<p style='font-size: 17px;'>Email from <strong>{0}</strong> ({1})</p>" +
                "<p>Date: {2}</p>" +
                "<p>Phone: {3}</p>" +
                "<p>Message:</p><p style='margin-left: 24px;'>{4}</p>" +
                "</div>";
            string to = ConfigurationManager.AppSettings["ContactUsEmailAddress"];
            MailMessage message = new MailMessage();
            message.To.Add(new MailAddress(to));
            message.Subject = "Message from " + inputModel.Name;
            message.Body = String.Format(body, new string[]
                {
                    inputModel.Name, inputModel.Email, DateTime.Now.ToLongDateString(), inputModel.Phone, inputModel.UserMessage
                }
            );
            message.IsBodyHtml = true;

            using (var smtp = new SmtpClient())
            {
                await smtp.SendMailAsync(message);
                // the "true" parameter in the constructor just sets a "Message sent" 
                // confirmation message in the view model that is displayed on the view 
                // via Razor.
                return View(new ContactUsViewModel(true));
            }
        }
        else
        {
            return View(inputModel);
        }
    }
    catch (Exception ex)
    {
        string ourEmailAddress = ConfigurationManager.AppSettings["ContactUsEmailAddress"];
        inputModel.PublicErrorMessage = "There was a problem sending your message. Please send an email directly to " +
            "<a href='mailto:" + ourEmailAddress + "'>" + ourEmailAddress + "</a> so we can hear from you :)";
        inputModel.InternalErrorMessage = ex.Message;
        return View(inputModel);
    }
}

如果这是相关的,这里也是我的ContactUsViewModel

public class ContactUsViewModel : BaseViewModel
{
    public ContactUsViewModel() { }
    public ContactUsViewModel(bool messageSent)
    {
        this.MessageSentConfirmation = "Your message has been sent. We will get back to you shortly!";
    }

    [Required(ErrorMessage = "Please include your name.")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Please enter a valid email address.")]
    [EmailAddress(ErrorMessage = "Please enter a valid email address.")]
    public string Email { get; set; }

    [Phone(ErrorMessage = "Please enter a valid phone number.")]
    public string Phone { get; set; }

    [Required(ErrorMessage = "Please enter a message.")]
    public string UserMessage { get; set; }

    public string MessageSentConfirmation { get; private set; }
}

编辑:我知道Post-Redirect-Get design pattern在技术上会绕过这个问题,但它并没有真正解决无法以空视图返回相同视图的技术限制模型。因此,我不认为将PRG作为解决方案。

1 个答案:

答案 0 :(得分:1)

这是来自评论部分的@StephenMuecke的解决方案。在我的ModelState.Clear()语句解决问题之前,在我的控制器方法中执行return