[英]Server side validation is done with data annotations instead of doing client side validation
[英]Client side validation using Data Annotations
我在将客户端验证与数据注释结合使用时遇到问题。 该项目正在使用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调用动态加载此用户控件的get:
$('#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。
还是有人对如何实现这一目标有其他想法? 我真的很想使用数据注释。
提前致谢!
它可以是一种解决方案http://xhalent.wordpress.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/
特别是,这个问题的复本非干扰式验证不适用于动态添加的局部视图 ,该参考也指XHalent博客
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.