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

  • Home
  • SEARCH
  • 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 8367207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:06:04+00:00 2026-06-09T13:06:04+00:00

I have a model called Foo which has a property called MyProp of type

  • 0

I have a model called Foo which has a property called MyProp of type Bar.
When I post this model to the controller I want the model binder to validate MyProp because it has the Required attribute just as it does with a string. I need this to be self-contained within the Bar class or as a separate class. I have tried to use the IValidatableObject on the Bar class but it seems like it’s impossible to check if the Foo class has the Required attribute on MyProp? So now I’m out of options and need some help. Below is some sample code for my question.

public class Foo {
    [Required]
    public Bar MyProp { get; set; }
}

public class Bar {
    [ScaffoldColumn(false)]
    public int Id { get; set; }
    public string Name { get; set; }
}
  • 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-09T13:06:06+00:00Added an answer on June 9, 2026 at 1:06 pm

    Here is one solution to my problem where I can use the built in required attribute and still get custom behavior. This is just some proof of concept code.

    The model:

    public class Page : IPageModel {
        [Display(Name = "Page", Prompt = "Specify page name...")]
        [Required(ErrorMessage = "You must specify a page name")]
        public PageReference PageReference { get; set; }
    }
    

    The model binder:

    public class PageModelBinder : DefaultModelBinder {
        protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) {
            foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(bindingContext.ModelType)) {
                var attributes = property.Attributes;
                if (attributes.Count == 0) continue;
                foreach (var attribute in attributes) {
                    if (attribute.GetType().BaseType == typeof(ValidationAttribute) && property.PropertyType == typeof(PageReference)) {
                        var pageReference = bindingContext.ModelType.GetProperty(property.Name).GetValue(bindingContext.Model, null) as PageReference;
                        Type attrType = attribute.GetType();
                        if (attrType == typeof (RequiredAttribute) && string.IsNullOrEmpty(pageReference.Name)) {
                            bindingContext.ModelState.AddModelError(property.Name,
                                ((RequiredAttribute) attribute).ErrorMessage);
                        }
                    }
                }
            }
            base.OnModelUpdated(controllerContext, bindingContext);
        }
    }
    

    The model binder provider:

    public class InheritanceAwareModelBinderProvider : Dictionary<Type, IModelBinder>, IModelBinderProvider {
        public IModelBinder GetBinder(Type modelType) {
            var binders = from binder in this
                          where binder.Key.IsAssignableFrom(modelType)
                          select binder.Value;
    
            return binders.FirstOrDefault();
        }
    }
    

    And last the global.asax registration:

    var binderProvider = new InheritanceAwareModelBinderProvider {
        {
            typeof (IPageModel), new PageModelBinder() }
        };
    ModelBinderProviders.BinderProviders.Add(binderProvider);
    

    The result: http://cl.ly/IjCS

    So what do you think about this solution?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model called Nonsense which has_one of Foo and Bar In my
I have a model called Answer which has a ForeignKey relationship to another model
In GAE, I have a model called Foo, with existing entities, and attempt to
I have a model called Event and another called Product. An event has many
I have a model called Business , and I want to save Business.all into
I have a model called Person. I want User to inherit from Person. class
I have a model called Contact which has_one guest like so. class Contact <
I have a model called Details, and two controller methods new and create. New
I want to create user-specific validations. User has a column called rule_values which is
Have a model called contact_email.date_sent I want to be able to run a report

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.