Asp.net mvc 3 MVC3-客户端验证
即使文本区域为空,我的表单也会发布到操作。在post操作中,我将此设置为null 此外,我还有一个DI、存储库和服务体系结构 我遵循了下面的步骤,但仍然不走运 以下是我到目前为止的情况: 企业实体Asp.net mvc 3 MVC3-客户端验证,asp.net-mvc-3,validation,jquery-validate,Asp.net Mvc 3,Validation,Jquery Validate,即使文本区域为空,我的表单也会发布到操作。在post操作中,我将此设置为null 此外,我还有一个DI、存储库和服务体系结构 我遵循了下面的步骤,但仍然不走运 以下是我到目前为止的情况: 企业实体 namespace Intranet.BusinessEntities { public partial class AnnualReportMessage { public string Message { get; set; } public
namespace Intranet.BusinessEntities
{
public partial class AnnualReportMessage
{
public string Message { get; set; }
public int AnnualReportYear { get; set; }
public string Fice { get; set; }
}
}
**Following is in Validations Folder**
using System.ComponentModel.DataAnnotations;
namespace Intranet.BusinessEntities
{
[MetadataType(typeof(AnnualReportMessageMetaData))]
public partial class AnnualReportMessage
{
private class AnnualReportMessageMetaData
{
[Required]
public string Message { get; set; }
}
}
}
布局中引用了以下2项
<script type="text/javascript" src="@Url.Script("jqueryMain/jquery.validate.js")"></script>
<script type="text/javascript" src="@Url.Script("jqueryMain/jquery.validate.unobtrusive.min.js")"></script>
formSubmit: function ($form, currentForm) {
if ($form.validate().form()) {
var $button = $("#" + AnnualReportSpecialEcMessage.enums.submitButtonId);
jMessage("Processing request...", $button, true, false);
$.ajax({
cache: false,
url: currentForm.action,
type: currentForm.method,
data: $form.serialize(),
error: function (xhr, ajaxOptions, thrownError) {
jMessageError(xhr.responseText, $button, false, true);
},
success: function (result) {
if (result.IsError) {
jMessageError(result.Message, $button, false, true);
}
else {
jMessageOK(result.Message, $button, false, false);
jMessageHideInterval(3000); //hide after 3 seconds
}
}
});
}
}
我在这里遗漏了什么吗?我认为您需要这样做:
[MetadataType(typeof(AnnualReportMessage.AnnualReportMessageMetaData))]
您是否有其他的AnnualReportMessageMetaData类?请使用此类。它将删除MVC3Razor中的默认验证方法
$(document).ready(function () {
var form = $('#formId').get(0);
$.removeData(form, 'validator');
});
然后,我们的jquery验证代码将正常工作问题出在jquery.validate.js上。要么买最新的,要么 替换
return $([]).add(this.currentForm.elements)
.filter(":input")
与
全工件
elements: function() {
var validator = this,
rulesCache = {};
// select all valid inputs inside the form (no submit or reset buttons)
// workaround $Query([]).add until http://dev.jquery.com/ticket/2114 is solved
//return $([]).add(this.currentForm.elements)
//.filter(":input")
return $(':input', this.currentForm)
.not(":submit, :reset, :image, [disabled]")
.not( this.settings.ignore )
.filter(function() {
!this.name && validator.settings.debug && window.console && console.error( "%o has no name assigned", this);
// select only the first element for each name, and only those with rules specified
if ( this.name in rulesCache || !validator.objectLength($(this).rules()) )
return false;
rulesCache[this.name] = true;
return true;
});
},
这是呈现的HTML还是实际的HTML?我有点惊讶它甚至可以编译,将您的元数据类作为私有嵌套类。FireFox运行正常。是IE 8出现了这个问题…不是。我已经把它改成了公共的,但还是一样的效果。此外,我现在不包括2项。当我查看呈现的html时,fice和AnnualReportYear的值为空,但这些post为0。[MetadataType(typeof(AnnualReportMessageMetaData))]公共部分类AnnualReportMessage{[Bind(Exclude=“AnnualReportYear,Fice”)]公共类AnnualReportMessageMetaData{[Required(ErrorMessage=“Message不能为空”)]公共字符串消息{get;set;}}我已经对元数据进行了更改,还公开了嵌套元数据类,但它仍然在做同样的事情。还有一件事我现在很困惑,那就是1。为什么我需要第二个隐藏字段。2.绑定的项不包括,其中一个项被渲染为非必需项,而另一个项被渲染为必需项。3.在发布的表单上,Message_Fice为null,Message_AnnualReportYear为0,其中两者都有值。[MetadataType(typeof(AnnualReportMessage.AnnualReportMessageMetaData))]公共部分类AnnualReportMessage{[Bind(Exclude=“AnnualReportYear,Fice”)]公共类AnnualReportMessageMetaData{[Required(ErrorMessage=“Message不能为空”)]公共字符串消息{get;set;}}FireFox运行正常,问题出在IE 8上。FF仍然存在隐藏字段Message\u Fice和Message\u AnnualReportYear未正确传递的问题。FF中的验证正常。不,IE 8没有运行。FF和IE9显示了验证消息并发布了表单。
return $([]).add(this.currentForm.elements)
.filter(":input")
return $(':input', this.currentForm)
elements: function() {
var validator = this,
rulesCache = {};
// select all valid inputs inside the form (no submit or reset buttons)
// workaround $Query([]).add until http://dev.jquery.com/ticket/2114 is solved
//return $([]).add(this.currentForm.elements)
//.filter(":input")
return $(':input', this.currentForm)
.not(":submit, :reset, :image, [disabled]")
.not( this.settings.ignore )
.filter(function() {
!this.name && validator.settings.debug && window.console && console.error( "%o has no name assigned", this);
// select only the first element for each name, and only those with rules specified
if ( this.name in rulesCache || !validator.objectLength($(this).rules()) )
return false;
rulesCache[this.name] = true;
return true;
});
},