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 7014819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:34:16+00:00 2026-05-27T22:34:16+00:00

Net MVC 3 for development. Now there is a scenario where i need to

  • 0

Net MVC 3 for development.
Now there is a scenario where i need to create to a custom validator on a collection property.
Now the problem I am facing is that this custom validator works well when the length of the collection is 1 but when there are more than one elements in the collection i face a problem is the custom validation pushes errors but the problem occurs that when checked in ModelState the error is not found plugged. So in this case what should i do ? I am pasting in the code as below.

This is the Model i want to validate:

public class CreateTestModel
{
    [Required]
    public string Name { get; set; }

    public string Description { get; set; }

    public RadioButtonListViewModel<GoalTypes> Goals { get; set; }

    [MinimumRequired("IncludedEntities", "ExcludedEntities", 1, ErrorMessage = "1   Entity is compulsory")]
    public IEnumerable<TestEntityModel> IncludedEntities { get; set; }

    public IEnumerable<TestEntityModel> ExcludedEntities { get; set; }

    public IEnumerable<TestFilterModel> IncludedFilters { get; set; }

    public IEnumerable<TestFilterModel> ExcludedFilters { get; set; }

    public IEnumerable<BucketModel> Buckets { get; set; }

    public bool AutoDecision { get; set; }

    public DateTime StartDate { get; set; }

    public DateTime EndDate { get; set; }

    public int AdminId { get; set; }

    [DefaultValue(true)]
    public bool IsEnabled { get; set; }
  }

Purpose of the Custom Validator:-
I need to validate minimum number of required entities in a collection.
There are some scenarios when for eg in this case : The IncludedEntities and the ExcludedEntities needs to have a minimum count of 1 including both the properties ,this is my business rule.
So for this the Below custom Validator is written.

This is the Custom Validator i have written :

  [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class MinimumRequired : ValidationAttribute,IClientValidatable
{
    public int numberOfMandatoryEntities{get; private set;}
    public int totalCountofIncludeEntities { get; private set; }
    public bool isBucket { get; set; }
    public string Property1{get; private set;}
    public string Property2{ get; private set; }
    private const string DefaultErrorMessageFormatString = "Atleast one entity is required";

    public MinimumRequired(string Property1, string Property2, int MandatoryCount)
    {
        this.Property1 = Property1;
        if (!String.IsNullOrEmpty(Property2))
            this.Property2 = Property2;
        numberOfMandatoryEntities = MandatoryCount;
    }


    public override string FormatErrorMessage(string name)
    {
        return string.Format(ErrorMessageString, name);
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        object property2Value = null;
        object property1Value = null;
        int property1Count=0;
        if(!String.IsNullOrEmpty(Property2))
        property2Value = validationContext.ObjectInstance.GetType().GetProperty(Property2).GetValue(validationContext.ObjectInstance, null);
        property1Value = validationContext.ObjectInstance.GetType().GetProperty(Property1).GetValue(validationContext.ObjectInstance, null);
        if (property1Value != null)
        {
        property1Count = ((IEnumerable<Object>)property1Value).Count();
        }

        if (property2Value != null)
        {
            property1Count = property1Count + ((IEnumerable<Object>)property2Value).Count();
        }
        if (property1Count < numberOfMandatoryEntities)
        {
            return new ValidationResult(ErrorMessage);
        }

        return ValidationResult.Success;
    }
    #region IClientValidatable Members

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var x = new ModelClientValidationRule();
        x.ValidationType = "entityvalidator";
        x.ErrorMessage = string.Format(ErrorMessageString, metadata.GetDisplayName());
        x.ValidationParameters.Add("mandatoryentity", numberOfMandatoryEntities);
        return new[] 
        {  
            x
        };
    }

    #endregion



}

Please help me out…

  • 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-05-27T22:34:16+00:00Added an answer on May 27, 2026 at 10:34 pm

    I triend the above code it works perfectly for me.

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

Sidebar

Related Questions

I have an Asp.Net MVC application that works in the vs.net development web server.
I have an ASP.NET MVC 2 application in development that uses a web service
I have this ASP.NET MVC application that I now want to separate out the
This question is related to my ASP.NET MVC 2 development, but it could apply
My new ASP.NET MVC Web Application works on my development workstation, but does not
Im currently using vs2008 with asp.net mvc framework for web development. Im missing a
we have a mixed development environment using ASP.NET MVC and Ruby on Rails. We
NET MVC and i want to create a class to contain site wide functions
I noticed the following: An ASP.NET MVC website under development gets an SQL error
I'm getting back into .NET development after a couple years and it seems that

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.