2014-09-13 72 views
0

所以我尝试使用以下DataAnnotations:数据注释不工作不显眼的jQuery验证在部分ASP.NET MVC

[Required(ErrorMessage = "A Description Is Required.")] 
    [StringLength(500, ErrorMessage = "The Description must be at least {2} characters long.", MinimumLength = 2)] 
    public string description { get; set; } 

的DataAnnotations填充在我<input />元素响应客户对我的描述性数据属性:

<input maxlength="500" class="form-control" data-val="true" data-val-length="The Description must be at least 2 characters long." data-val-length-max="500" data-val-length-min="2" data-val-required="A Description Is Required." id="Add_description" name="Add.description" required="required" type="text" value=""> 

我引用脚本/ JS库:

jQuery Validation Plugin 1.13.1 
Microsoft jQuery Unobtrusive Validation 3.2.2 
Microsoft jQuery Unobtrusive Ajax 3.2.2 

我不确定我是否需要Microsoft jQuery Unobstrusive Ajax库,但是我读了answer,因为我也在尝试Range验证,但它不起作用,但我将重点放在了为此问题描述属性的必需消息上,以保留它很简单。

这里是我查看代码(请理解我的引导模式是HTML语法正确我刚才排除一些我自举模式HTML元素使代码更易于读取,我的问题):

@using (Html.BeginForm("CustomFieldAddNew", "CustomField", null, FormMethod.Post, new { @role = "form", id = "CustomFieldAddForm", enctype = "multipart/form-data" })) 
     { 
      @Html.HiddenFor(mi => mi.Add.custom_field_context_id) 

      <div class="modal-body"> 

       <div class="table-responsive"> 
        <table class="table table-striped table-bordered"> 
         <thead> 
          <tr> 
           <th colspan="2">General Information </th> 
          </tr> 
         </thead> 
         <tbody> 
          <tr> 
           <td> 
            <label for="">Description<i class="icon-asterisk"></i></label> 
           </td> 
           <td> 
            <div class="col-md-8 padding-0"> 
             @Html.TextBoxFor(mi => mi.Add.description, new { @class = "form-control", @MaxLength = "500", required = "required" }) 
             @Html.ValidationMessageFor(mi => mi.Add.description) 
            </div> 
           </td> 
          </tr> 
          </tbody> 
         </table> 
        </div> 
       </div> 
      </div> 
      <!-- /.modal-body --> 
      <div class="modal-footer"> 
       <button id="btnCloseSaveModal" type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       <button id="btnCustomFieldSave" type="button" class="btn btn-primary">Save</button> 
      </div> 
     } 

<script> 
     $('#btnCustomFieldSave').on('click', function() { 
     var $form = $(this).parents('form'); 

     var postData = { 
      description: $('#@Html.IdFor(mi => mi.Add.description)').val() 
     } 

     if ($form.validate().form()) {//fire jQuery validation 
      $.ajax({ 
       type: "POST", 
       url: $form.attr('action'), 
       contentType:"application/json", 
       data: JSON.stringify(postData), 
       error: function (xhr, status, error) { 
        toastr.error("Error saving, please try again."); 
       }, 
       success: function (data) { 
        $('#customField').modal('toggle'); 
        $form.trigger("reset"); 
       } 
      }); 
     } 

     return false; 
    }); 
}); 
</script> 

我的客户端验证消息应该说:

"A Description Is Required." 

但是相反,当我点击btnCustomFieldSave,和我的jQuery CLICK监听功能火灾和调用$ form.validate()验证错误消息显示“此网络连接现场是必需的。“这是jQuery验证插件1.13.1库中的默认消息。

我的问题是,为什么我的DataAnnotation消息“需要说明的是必需的”,而取代罐头jQuery验证插件的消息不显示“这是必须填写”

注意,我是否包含了线@Html.ValidationMessageFor(mi => mi.Add.description)或不。我仍然看到“此字段是必需的”,而不是我在DataAnnotation属性Required中定义的自定义消息,即“需要说明”

回答

4

因此,Microsoft jQuery Unobtrusive Validation已经解雇了它的解析并绑定到数据attrs我的部分表单内容已加载。

我需要再有微软的jQuery不显眼的验证解析火,所以在底部我偏我说:

$(function() { 
     //Microsoft Unobtrusive jQuery validation has already binded so must kick off parse to bind partial loaded dynamic form 
     $.validator.unobtrusive.parse($('#CustomFieldAddForm')); 
}); 

我在这里找到了答案:Data Annotations/Validation not working for partial views

相关问题