我有一个checkBoxFor用于JQuery表单并绑定到我的模型,我的问题是,当用户检查或取消选中该框时,用于某些客户端验证的值始终为true,并且用于服务器端的值验证总是错误的。为什么我的复选框没有更新值?
查看:
<form id="companyForm">
<fieldset>
<p>
@Html.LabelFor(model => model.allCompany, new { @checked = "checked" })
@Html.CheckBoxFor(model => model.allCompany)
</p>
<p>
@Html.LabelFor(model => model.hierarchyValidation)
@Html.DisplayFor(model => model.hierarchyValidation)
@Html.HiddenFor(model => model.hierarchyValidation)
@Html.ValidationMessageFor(model => model.hierarchyValidation)
</p>
<!-- Allow form submission with keyboard without duplicating the dialog button -->
<input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
</fieldset>
</form>
查看模型
公共类AXAddRoleViewModel {
public bool allCompany { get; set; }
[Display(Name = "HierarchySelectList")]
[IsEmptyAttribute("hierarchyValidation", "allCompany")]
public string hierarchyValidation { get; set; }
}
客户端验证:
var isApplicationValid = $('#application').valid();
var isHierarchyValid = $('#hierarchyValidation').valid();
if (isHierarchyValid && isCompanyValid) {
var roleName = $("#roleName").val();
var hierarchy = $("#hierarchyValidation").val().toString();
var data = {
"reasons": message,
"hierarchyValidation": hierarchy
};
$.ajax({
url: url,
type: "POST",
data: data,
success: function (data, textStatus, jqXHR) {
alert("Success");
},
error: function (jqXHR, textStatus, errorThrown) {
alert("Error");
}
});
}
}
自定义验证器属性:
public enum Comparison
{
IsEmpty,
ContainsValue
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class IsEmptyAttribute : ValidationAttribute, IClientValidatable
{
private const string DefaultErrorMessage = "{0} Is required if a value from {1} is not selected.";
public string ValueLabel { get; private set; }
public string CompaniesValue { get; set; }
public IsEmptyAttribute(string valueLabel, string companiesValue)
: base(DefaultErrorMessage)
{
if (string.IsNullOrEmpty(valueLabel))
{
throw new ArgumentNullException("otherProperty");
}
ValueLabel = valueLabel;
CompaniesValue = companiesValue;
}
public override string FormatErrorMessage(string name)
{
return string.Format(ErrorMessageString, ValueLabel, CompaniesValue);
}
protected override ValidationResult IsValid(object value,
ValidationContext validationContext)
{
var allCompaniesSelected = validationContext.ObjectInstance.GetType()
.GetProperty(CompaniesValue);
var allCompaniesSelectedValue = allCompaniesSelected
.GetValue(validationContext.ObjectInstance, null);
if (Convert.ToBoolean(allCompaniesSelectedValue) == false)
{
if (value == null)
{
return new ValidationResult(
FormatErrorMessage(validationContext.DisplayName));
}
}
return ValidationResult.Success;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var clientValidationRule = new ModelClientValidationRule()
{
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
ValidationType = "isempty"
};
clientValidationRule.ValidationParameters.Add("companiesvalue", CompaniesValue);
return new[] { clientValidationRule };
}
}
自定义验证器javascript:
(function ($) {
$.validator.addMethod("isempty", function (value, element, params) {
if (this.optional(element)) {
var allCompanies = $('#addRoleCompany_allCompany')
if (allCompanies.val() == false) {
var otherProp = $('#addRoleCompany_' + params)
return (otherProp.val() != value);
}
}
return true;
});
$.validator.unobtrusive.adapters.addSingleVal("isempty", "otherproperty");
}(jQuery));
有人能告诉我在哪里可以找到问题以及问题是什么?
感谢您对此问题的任何帮助。
答案 0 :(得分:1)
事实证明,我忽略了这样一个事实,即我需要在检查框是否勾选后从Jquery传递值。
一个简单的错误。谢谢大家帮我解决这个问题。