C# 使用数据注释的客户端验证
我在结合使用客户端验证和数据注释时遇到问题。 该项目使用ASP.NETFramework4.5,这是一个具有MVP模式和jQueryAjax调用的Web应用程序,用于动态加载用户控件 这是(剥离)用户控件,其中包含需要验证的输入元素:C# 使用数据注释的客户端验证,c#,asp.net,jquery,data-annotations,C#,Asp.net,Jquery,Data Annotations,我在结合使用客户端验证和数据注释时遇到问题。 该项目使用ASP.NETFramework4.5,这是一个具有MVP模式和jQueryAjax调用的Web应用程序,用于动态加载用户控件 这是(剥离)用户控件,其中包含需要验证的输入元素: <form id="insertArtist" class="form-horizontal"> <fieldset> <legend>Add new artist</legend> <div clas
<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>
这是用户单击“保存”时执行的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
或者有人对如何实现这一点有其他想法吗?我真的很想使用数据注释
提前谢谢 这可能是一个解决方案
特别是,这是一个双重的问题,也涉及到XHalent博客
Page_ClientValidate is not defined