requiredIf 使用子实体进行验证
我正在尝试使用此处的属性让我的 MVC3 应用程序显示RequiredIfs 的验证消息: http ://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx 现在,如果没有子实体,但我有一个用户,并且该用户地址对象需要验证,它就可以正常工作。实际上,即使是一个地址也可以,但有 3 个地址,每个地址都有一个复选框,指示它是否处于活动状态。
我尝试按如下方式更改 validator.unobtrustive.js 的 jQuery,以获取验证时的正确值(地址是“Address_”或“BillingAddress_”:
$.each(this.params, function () {
var address = element.name.split('.')[0] + '_';
paramValues[this] = this.toString() == 'dependentproperty' ? address + $element.attr(prefix + this) : $element.attr(prefix + this);
});
这仅在选中所需If 所依赖的所有可能的复选框时才有效。以下验证所有地址并在视图上输出消息:
$.each(this.params, function () {
paramValues[this] = this.toString() == 'dependentproperty' ? 'Address_' + $element.attr(prefix + this) : $element.attr(prefix + this);
});
欢迎任何帮助或想法为什么会这样=)解决问题的其他方法我会很乐意接受。感谢您的帮助。
编辑
我意识到 javascript 的东西无论如何都不起作用,现在我已经从模型中获得了正确的 ID。但我仍然有一个错误,即它仅在选中所有复选框时才验证。有什么想法吗?
地址的模型构建如下:
[RequiredIf("IsActive", true)]
[Display(Name = UserManagementResources.sdAddressText, ResourceType = typeof(UserManagementResources))]
public string Street
{
get { return _address["sdAddress"]; }
set { _address["sdAddress"] = value; }
}
[RequiredIf("IsActive", true)]
[Display(Name = UserManagementResources.sdCityText, ResourceType = typeof(UserManagementResources))]
public string City
{
get { return _address["sdCity"]; }
set { _address["sdCity"] = value; }
}
ViewModel 包含不同的地址。
public Address Address
{
get { return _address ?? (_address = new Address(Customer.Name, _domain, "Address") { IsActive = true }); }
}
public Address BillingAddress
{
get { return _billingAddress ?? (_billingAddress = new Address(Customer.Name, _domain, "BillingAddress")); }
}
控制器只是创建一个新的空模型。该视图使用 Html.EditorFor() 显示不同的地址。
I am trying to get my MVC3 application to display the validation messages of RequiredIfs, using the attribute from here:
http://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx
Now it works fine if there are no child entities, but I have a user, and that users address object needs to be validated. Well actually even one address would work, but there are 3 addesses, each has a checkbox that indicates whether it is active or not.
I tried changing the jQuery of the validator.unobtrustive.js as following to get the correct value on validation (address is either 'Address_' or 'BillingAddress_':
$.each(this.params, function () {
var address = element.name.split('.')[0] + '_';
paramValues[this] = this.toString() == 'dependentproperty' ? address + $element.attr(prefix + this) : $element.attr(prefix + this);
});
This only works when all possible checkboxes that a RequiredIf depends on is checked. The following validates all addesses and gives outputs the messages on the view:
$.each(this.params, function () {
paramValues[this] = this.toString() == 'dependentproperty' ? 'Address_' + $element.attr(prefix + this) : $element.attr(prefix + this);
});
Any help or ideas why it is behaving this way are welcome =) Other approches to solve the problem I'll happily accept. Thanks for your help.
Edit
I realized that the javascript stuff doesn't work anyways and am now already getting the correct ID from the model. But I still have the error that it only validates when all the checkboxes are checked. Any ideas why that is?
The Model for an Address is built as following:
[RequiredIf("IsActive", true)]
[Display(Name = UserManagementResources.sdAddressText, ResourceType = typeof(UserManagementResources))]
public string Street
{
get { return _address["sdAddress"]; }
set { _address["sdAddress"] = value; }
}
[RequiredIf("IsActive", true)]
[Display(Name = UserManagementResources.sdCityText, ResourceType = typeof(UserManagementResources))]
public string City
{
get { return _address["sdCity"]; }
set { _address["sdCity"] = value; }
}
The ViewModel contains the different addresses.
public Address Address
{
get { return _address ?? (_address = new Address(Customer.Name, _domain, "Address") { IsActive = true }); }
}
public Address BillingAddress
{
get { return _billingAddress ?? (_billingAddress = new Address(Customer.Name, _domain, "BillingAddress")); }
}
The controller simply creates a new Empty Model. The view displays the different addresses using a Html.EditorFor().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我假设您的公共地址属性具有设置和获取(上面未显示)。一种方法是在用户取消选中“IsActive”时禁用地址编辑表单。然后使用
ignore: ':disabled'
的 jQuery 验证选项,或者如果隐藏它们,ignore: ':hidden'
。查看使用以下内容的示例:http://jsfiddle.net/2EfPZ/21/
这个问题:
获取尝试使用 jQuery Validate 插件验证依赖于另一个选项的字段时出错
Well, I assume your public Address properties have sets as well as gets (not shown above). One way to do this is to just disable the address editing form when the user unchecks "IsActive". Then use the jQuery validate option of
ignore: ':disabled'
or if you hide them,ignore: ':hidden'
. Check out this sample that uses that:http://jsfiddle.net/2EfPZ/21/
From this question:
Getting an error when trying to validate fields that depend on another option using jQuery Validate plug-in