ASP.NET 未显示 MVC 验证错误

ASP.NET MVC Validation errors not displaying

我正在使用 tuples 将多个模型加载到一个视图中。

视图如

 @using (Ajax.BeginForm(null, null, ajaxOptions, new { @class = "contact-form" }))

        {
           @Html.AntiForgeryToken()
           <div class="form-group form-group--xs">
              @Html.TextBoxFor(m => m.Item2.Email, new { @class = "form-control input-sm", placeholder = "Your email address..." })
              @Html.ValidationMessageFor(m => m.Item2.Email)
           </div>
           <div class="form-group form-group--xs">
              @Html.TextAreaFor(m => m.Item2.Message, new { @class = "form-control input-sm", placeholder = "Your message...", rows = "4" })
              @Html.ValidationMessageFor(m => m.Item2.Message)
           </div>

           <input type="submit" class="btn btn-primary-inverse btn-sm btn-block" value="Send Your Message" />
        }

控制器是

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<PartialViewResult> Submit([Bind(Prefix = "Item2")] ContactForm model)
        {
            bool isMessageSent = true;

            if (ModelState.IsValid)
            {
                try
                {
                    await Services.EmailService.SendContactForm(model);
                }
                catch (Exception ex)
                {
                    isMessageSent = false;
                }
            }
            else
            {
                isMessageSent = false;
            }
            return PartialView("_SubmitMessage", isMessageSent);
        }

联系表是

 public class ContactForm
    {
        [Required(ErrorMessage = "You must provide your email."), EmailAddress]
        public string Email { get; set; }

        [Required(ErrorMessage = "You must include a message.")]
        public string Message { get; set; }
    }

最后是局部视图

@model bool

@if (Model)
{
    <div style="color:#c2ff1f">Your message has been sent.</div>
}
else
{
    <div style="color:#f34141">An error occured while sending your message</div>
}

更新: 电子邮件文本框呈现的 html 是

<input class="form-control input-sm" 
data-val="true" 
data-val-email="The Email field is not a valid e-mail address." 
data-val-required="You must provide your email." 
id="Item2_Email" 
name="Item2.Email" 
placeholder="Your email address..." 
type="text" 
value="">

如果我没有在电子邮件字段或消息字段中输入任何文本,则不会发送消息 :) 但我在视图中没有验证错误 :( 我在这里做错了什么?

您返回的部分视图没有验证错误消息代码。

试试这个代码:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<PartialViewResult> Submit([Bind(Prefix = "Item2")] ContactForm model)
        {
            bool isMessageSent = true;

            if (ModelState.IsValid)
            {
                try
                {
                    await Services.EmailService.SendContactForm(model);
                }
                catch (Exception ex)
                {
                    isMessageSent = false;
                }

            }
            else
            {
                isMessageSent = false;
                //Return the view that has validation error message display code.
                return View(model);
            }
            return PartialView("_SubmitMessage", isMessageSent);
        }

我想我需要休息一下。我失踪了

@Scripts.Render("~/bundles/jqueryval")

对于像我这样的人,只需将这些脚本添加到您的视图中即可:

  1. jquery.validate
  2. jquery.validate.unobtrusive