簡體   English   中英

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="">

如果我沒有在電子郵件字段或消息字段中輸入任何文本,則不會發送消息 :) 但我在視圖中沒有驗證錯誤 :( 我在這里做錯了什么?

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

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

您正在返回一個沒有驗證錯誤消息代碼的 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);
        }

對於像我這樣的人,只需將這些腳本添加到您的視圖中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM