MVC帖子未返回正确的ViewResult

本文关键字:ViewResult 返回 MVC | 更新日期: 2023-09-27 18:00:09

在我的网站的联系人页面的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设计模式在技术上可以绕过这个问题,但它并没有真正解决无法使用空视图模型返回相同视图的技术限制。因此,我不认为实施PRG是解决方案。

MVC帖子未返回正确的ViewResult

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