2012-07-19 53 views
0

我在使用客戶端驗證與數據註釋相結合時遇到問題。 該項目使用ASP.NET Framework 4.5,一個帶有MVP模式的Web應用程序和jQuery Ajax調用來動態加載用戶控件。使用數據註釋進行客戶端驗證

這是包含需要被驗證所述輸入元件中的(剝離的)用戶控制:

<form id="insertArtist" class="form-horizontal"> 
<fieldset> 
<legend>Add new artist</legend> 
    <div class="control-group"> 
     <label class="control-label" for="name">Name</label> 
     <div class="controls"> 
      <asp:TextBox runat="server" ID="name" ClientIDMode="Static" CssClass="input-medium"></asp:TextBox> 
      <ml:DataAnnotationValidator runat="server" ID="davName" ControlToValidate="name" EnableClientScript="true" PropertyToValidate="Name" SourceTypeName="Music.Library.Domain.Models.Artist, Music.Library.Domain"> 
     </ml:DataAnnotationValidator> 
    </div> 
    <div class="form-actions"> 
     <button id="save" class="btn btn-primary" name="submit" type="submit">Save</button> 
     <button id="cancel" class="btn">Cancel</button> 
    </div> 
</div> 
</form> 

這個用戶控件獲取的使用下面的AJAX調用動態加載:

$('#add').click(function() { 
    $.ajax({ 
     url: "/Artist/Manage/Insert.ascx.axd", 
     type: "POST", 
     dataType: "html", 
     success: function (obj) { 
      $('#body_artist').html($(obj)); 
     } 
    }); 
}); 

這執行jQuery的,當用戶點擊保存:

$('#save').click(function (event) { 
    event.preventDefault(); 

    $.ajax({ 
     url: "/artist/add.axd", 
     type: "POST", 
     dataType: "html", 
     cache: false, 
     async: true, 
     data: 'Ajax=true&' + $('#insertArtist').serialize(), 
     beforeSend: function (jqXHR, settings) { 
      $('#loading').show(); 
     }, 
     success: function (data) { 
      if (data == "succes") { 
       showNotification('succes', 'Succesfully added artist'); 
      } else { 
       showNotification('error', 'Error while adding artist: ' + data); 
      } 
     }, 
     error: function (jqXHR, textStatus, errorThrown) { 
      showNotification('error', 'Error while adding artist: ' + data); 
     }, 
     complete: function() { 
      $('#loading').hide(); 
     } 
    }); 
}); 

現在,由於asp.net控件沒有觸發器,所以自定義數據註釋驗證器將不會進行驗證。

我試着驗證使用Javascript,使用Page_ClientValidate(),ValidateEnable()和Validate()方法。不幸的是,當我嘗試這個時,我一直得到同樣的錯誤:

Page_ClientValidate is not defined 

其他方法的情況也是如此。 我在這裏結束了。

是否因爲UserControl是動態加載的,這些客戶端驗證方法不起作用?我已將EnableClientScript設置爲true。

還有沒有人有如何實現這個另一個想法?我真的很想使用數據註釋。

在此先感謝!

回答

相關問題