必需的验证错误消息未以 jquery 弹出模式形式显示

Posted

技术标签:

【中文标题】必需的验证错误消息未以 jquery 弹出模式形式显示【英文标题】:Required Validation Error Message is not getting displayed in jquery popup modal form 【发布时间】:2013-03-10 12:43:53 【问题描述】:

我使用ComponentModel.DataAnnotations [必需] 属性来验证消息。该消息未显示在我的表单中。但是,正则表达式验证和范围验证工作正常。

在我的一个领域中,我同时使用了 [Required] 和 [RegualarExpression]。当我在文本框中输入错误时,会出现 RegualarExpression 验证消息,删除输入后,只有必需的验证消息。否则,不会出现必需的验证消息。

这是我的代码:

--模型类

    public class Email
    
    [Required(ErrorMessage = "Email Address is required!")]
    [RegularExpression("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]1,3\\.[0-9]1,3\\.[0-9]            1,3\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]2,4|[0-9]1,3)(\\]?)$", ErrorMessage = "Invalid Email Address.")]
    public virtual string EmailAddress  get; set; 


    [Required(AllowEmptyStrings = false, ErrorMessage = "First Name is Required")]
    public virtual string FirstName  get; set; 

    public virtual string MiddleName  get; set; 
    [Required(ErrorMessage = "Last Name is required!")]
    public virtual string LastName  get; set; 

    public virtual string Status  get; set; 
    

--查看

    @html.EnableClientValidation();

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">        </script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"     type="text/javascript"></script>
    @using (Html.BeginForm("CreateEmail", "LeaderBoard", FormMethod.Post, new  enctype =   "multipart/form-data", id = "create-email-dialog" ))
    
    @Html.ValidationSummary(true)
    
    <form>
    <table  border="0" cellspacing="0" cellpadding="0" class="popTable">
    <tr>
        <td >
            First Name:
        </td>
        <td >
            <div class="editor-field">
                @Html.EditorFor(model => model.FirstName)
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>
        </td>
    </tr>
    <tr>
        <td >
            Middle Name:
        </td>
        <td >
            <div class="editor-field">
                @Html.EditorFor(model => model.MiddleName)
                @Html.ValidationMessageFor(model => model.MiddleName)
            </div>
        </td>
    </tr>
    <tr>
        <td >
           Last Name:
        </td>
        <td >
            <div class="editor-field">
                @Html.EditorFor(model => model.LastName)
                @Html.ValidationMessageFor(model => model.LastName)
            </div>
        </td>
    </tr>
    <tr>
        <td >
            Email Address:
        </td>
        <td >
            <div class="errorMessage">
                @Html.EditorFor(model => model.EmailAddress)
                @Html.ValidationMessageFor(model => model.EmailAddress)
            </div>
        </td>
    </tr>
     <tr>
        <td >
             Status:
        </td>
        <td >
            <div class="editor-field">
                @Html.DropDownList("Status", ViewData["Status"] as SelectList)
            </div>
        </td>
    </tr>
    </table>    
    <input type="button" id="btnCreateEmail" value="Save" name="submit" class="btn" />
    <input type="button" value="Cancel" name="cancel" class="closeDialog" />
    </form>

    
    

--控制器

    [HttpGet]
    public ActionResult CreateEmail()
    
        if (Request.IsAjaxRequest())
        
                var list = new SelectList(new[]
                
                    newId = "Active", Name = "Active",
                    newId = "Inactive", Name = "Inactive"
                ,
                "Id", "Name", 1);

            ViewData["Status"] = list;
            return PartialView("_CreateEmail");
        
        return RedirectToAction("Index");
    

    [HttpPost]
    public ActionResult CreateEmail(Email model)
    
        using (_emailRepository)
        
            if (ModelState.IsValid)
            
                _emailRepository.Create(model);
                return Json(new  success = true );
            
            return RedirectToAction("Index");
        
    

【问题讨论】:

你期待什么?您需要同时显示两条消息吗? 我有点困惑?你在期待什么?您已将标题提到为 jquery 弹出模式,但从未在您的问题中使用它 我需要显示 [Required] 错误验证消息。我希望这条消息会显示在名字和姓氏等字段中。显示的表单是jquery popup modal。 您是说需要在页面加载本身上显示 [Required] 错误验证消息吗? 没有。我希望在选项卡更改或表单提交时这样做 【参考方案1】:

在文档就绪函数中验证这些字段

       $( document ).ready( function ( e )
        

$( "form" ).validate().element( "#FirstName" );


$( "form" ).validate().element( "#LastName" );
);

【讨论】:

以上是关于必需的验证错误消息未以 jquery 弹出模式形式显示的主要内容,如果未能解决你的问题,请参考以下文章

必填字段验证在 JQuery Popup MVC 4 中不起作用

必填字段验证在 JQuery Popup MVC 4 中不起作用

必填字段验证在 JQuery Popup MVC 4 中不起作用

必填字段验证在 JQuery Popup MVC 4 中不起作用

select2下拉菜单的jquery验证弹出位置问题

Ajax jQuery serialize() & serializeArray() textarea 未以 Django 形式提交