2011-08-10 54 views
13

我在驗證MVC3上有很多麻煩。它正常工作時,我只是直接加載頁面,但是當我使用jQuery的AJAX POST它不驗證:在jquery ajax上使用mvc 3進行不顯眼的驗證問題

這種形式的使用$('#modal-dialog').load('/DartsMVC/Restaurant/Edit/13')加載,這是渲染HTML:

<form action="/DartsMVC/Restaurant/Edit/13" method="post"> 
    <fieldset> 
     <legend>Restaurant</legend> 

     <input data-val="true" data-val-number="The field RestaurantID must be a number." data-val-required="The RestaurantID field is required." id="RestaurantID" name="RestaurantID" type="hidden" value="13"> 

     <div class="editor-label"> 
      <label for="Name">Name</label> 
     </div> 
     <div class="editor-field"> 
      <input class="text-box single-line" data-val="true" data-val-required="The Name field is required." id="Name" name="Name" type="text" value="furaibo"> 
      <span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span> 
     </div> 

     <p> 
      <input type="submit" value="Save" class="ui-button ui-widget ui-state-default ui-corner-all" role="button" aria-disabled="false"> 
     </p> 
    </fieldset> 
</form> 

AJAX POST是通過攔截表單/提交使用。模塊對話框彈出並在帖子完成後關閉。我想在發送帖子之前驗證表格:

// force change the submit data to an ajax POST (usually 'save', 'delete' button) 
$('#modal-dialog').delegate('form', 'submit', function (e) { 
    e.preventDefault(); 
    var form = $(this); 
    form.validate(); // does nothing 
    if (form.valid()) { 
     $.ajax({ 
      url: form.attr('action'), 
      type: "POST", 
      data: form.serialize(), 
      success: function (data) { 
       $('#modal-dialog').dialog('close'); 
      }, 
      error: function (jqXhr, textStatus, errorThrown) { 
       alert("Error '" + jqXhr.status + "' (textStatus: '" + textStatus + "', errorThrown: '" + errorThrown + "')"); 
      } 
     }); 
    } 
}); 

我是否因此做了些什麼? .valid()始終返回true。

我的web.config有:

我的頁面源有:

<link href="/DartsMVC/Content/themes/cupertino/jquery-ui-1.8.14.custom.css" rel="stylesheet" type="text/css"> 

<script src="/DartsMVC/Scripts/modernizr-1.7.min.js" type="text/javascript"></script> 
<script src="/DartsMVC/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script> 
<script src="/DartsMVC/Scripts/jquery-ui-1.8.14.custom.min.js" type="text/javascript"></script> 

<script src="/DartsMVC/Scripts/jquery.unobtrusive-ajax.js" type="text/javascript"></script> 
<script src="/DartsMVC/Scripts/jquery.validate.js" type="text/javascript"></script> 
<script src="/DartsMVC/Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script> 

我的模型有:

[Required] 
    public string Name { get; set; } 

更多的研究後,我認爲它可能有與動態控件有關:

回答

26

解決

調用$ .validator.unobtrusive.parse($( '形式'))做了招。我想驗證器需要重新創建動態內容

+0

謝謝!我剛纔想到了這個,但是forogt,並且很難再找到解決方案。 –

+0

@juhan_h你在哪裏可以在代碼中調用上面的$ .validator代碼。我可能有一個類似的問題:/ – Elisabeth

+1

@Elisa,我最終重新初始化不顯眼的驗證創建AddRemoveValidator功能:'//初始化不顯眼的驗證 \t \t addValidationToForm:功能(formToAdd){ \t \t \t jQuery.each(formToAdd,功能(I,形式){ \t \t \t \t $(形式).removeData( 「驗證」); \t \t \t \t $ .validator.unobtrusive.parse(形式); \t \t \t}); \t \t}, \t \t addValidationToFormRemoveFromOthers:功能(formToAdd,formsToRemoveFrom){ \t \t \t爲(VAR從formsToRemoveFrom){ \t \t \t \t $(從)。removeData( 「驗證」); \t \t \t} \t \t \t global.addValidationToForm(formToAdd); \t \t}' –

相關問題