Note that there are some explanatory texts on larger screens.

plurals
  1. PORequiredIf validation with child entities
    primarykey
    data
    text
    <p>I am trying to get my MVC3 application to display the validation messages of RequiredIfs, using the attribute from here: <a href="http://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx" rel="nofollow">http://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx</a> 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.</p> <p>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_':</p> <pre><code>$.each(this.params, function () { var address = element.name.split('.')[0] + '_'; paramValues[this] = this.toString() == 'dependentproperty' ? address + $element.attr(prefix + this) : $element.attr(prefix + this); }); </code></pre> <p>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:</p> <pre><code>$.each(this.params, function () { paramValues[this] = this.toString() == 'dependentproperty' ? 'Address_' + $element.attr(prefix + this) : $element.attr(prefix + this); }); </code></pre> <p>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.</p> <hr> <p>Edit</p> <p>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?</p> <p>The Model for an Address is built as following:</p> <pre><code>[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; } } </code></pre> <p>The ViewModel contains the different addresses.</p> <pre><code> 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")); } } </code></pre> <p>The controller simply creates a new Empty Model. The view displays the different addresses using a Html.EditorFor().</p> <hr>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload