requiredIf 使用子实体进行验证

发布于 2024-11-08 13:44:57 字数 2221 浏览 0 评论 0原文

我正在尝试使用此处的属性让我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2024-11-15 13:44:57

好吧,我假设您的公共地址属性具有设置和获取(上面未显示)。一种方法是在用户取消选中“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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文