ASP.NET MVC验证错误未显示

时间:2017-04-01 08:21:11

标签: c# asp.net asp.net-mvc validation asp.net-ajax

我正在使用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="">

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

3 个答案:

答案 0 :(得分:1)

我想我需要休息一下。我不见了

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

答案 1 :(得分:0)

您正在返回没有验证错误消息代码的partialView。

试试这段代码:

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

答案 2 :(得分:0)

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

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