Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7839323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:24:23+00:00 2026-06-02T15:24:23+00:00

I am using the example at The Complete Guide To Validation In ASP.NET MVC

  • 0

I am using the example at The Complete Guide To Validation In ASP.NET MVC 3 to create a RequiredIf validation attribute (it’s about 1/3 down the page under the heading of “A more complex custom validator“). It all works fine with the exception of one scenario, and that is if I have the need to validate against a complex type. For example, I have the following model:

public class MemberDetailModel
{
  public int MemberId { get; set; }
  // Other model properties here
  public MemberAddressModel HomeAddress { get; set; }
  public MemberAddressModel WorkAddress { get; set; }
}

public class MemberAddressModel
{
  public bool DontUse { get; set; }
  // Other model properties here

  [RequiredIf("DontUse", Comparison.IsEqualTo, false)]
  public string StreetAddress1 { get; set; }
}

The problem is that when the attribute validation for the StreetAddress property is rendered, it get’s decorated with the attribute of data-val-requiredif-other="DontUse". Unfortunately, since the address is a sub-type of the main model, it needs to be decorated with a name of HomeAddress_DontUse and not just DontUse.

Strangely enough, the validation works fine for server-side validation, but client-side unobtrusive validation fails with an JS error because JS can’t find the object with a name of just “DontUse”.

Therefore, I need to find a way to change the ModelClientValidationRequiredIfRule method to know that the property it is validating is a sub-type of a parent type, and if so, prepend the ParentType_ to the “otherProperty” field (e.g. otherProperty becomes HomeAddress_DontUse.

I have tried passing in typeof(MemberAddressModel) as a parameter of the attribute, but even when debugging the attribute creation, I can’t seem to find any reference to the parent type of HomeAddress or WorkAddress from that type.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:24:26+00:00Added an answer on June 2, 2026 at 3:24 pm

    Based on the suggestion from The Flower Guy, I was able to come up with the following which seems to work. I simply modified the following in the customValidation.js file:

    jQuery.validator.addMethod("requiredif", function (value, element, params) {
      if ($(element).val() != '') return true;
      var prefix = getModelPrefix(element.name); // NEW LINE
      var $other = $('#' + prefix + params.other);  // MODIFIED LINE
      var otherVal = ($other.attr('type').toUpperCase() == "CHECKBOX") ? ($other.attr("checked") ? "true" : "false") : $other.val();
      return params.comp == 'isequalto' ? (otherVal != params.value) : (otherVal == params.value);
    });
    

    I also added the following method to that file (within the JQuery block so as to be only privately accessible):

    function getModelPrefix(fieldName) {
      return fieldName.substr(0, fieldName.lastIndexOf(".") + 1).replace(".","_");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using jquery file upload to upload images to asp.net server (http://www.webtrendset.com/2011/06/22/complete-code-example-for-using-blueimp-jquery-file-upload-control-in-asp-net/) I need
Here is a complete example. I want to forbid using A::set from objects casted
Using Complete C++ i18n gettext() hello world example I changed the locale from es_MX
Using the example code included in the man page for DateTime::Astro::Sunrise , I'm getting
i have to create a program to login on facebook using example http://code.google.com/p/facebook-java-api/wiki/Examples but
I would like to replace just complete words using php Example : If I
Using this complete example I could draw text on Aero Glass. The rendering is
I was reading 'C++ Template complete guide' book, part about meta programming. There is
Is there a simple complete code example using any gui toolkit (that will work
I have a wizard in my asp.net MVC application which is built upon this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.