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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:13:18+00:00 2026-06-09T23:13:18+00:00

I have the following property in my Model : [StringLength(100, ErrorMessage = Must be

  • 0

I have the following property in my Model :

[StringLength(100, ErrorMessage = "Must be less than 100 Chars", MinimumLength = 3)]
public List<KeyValuePair<int, string>> Authors { get; set; }

How can I Validate each string into the above list with DataAnnotation Validation attribute in MVC3 ?

Is it possible at all ?

  • 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-09T23:13:20+00:00Added an answer on June 9, 2026 at 11:13 pm

    Custom validation to the rescue! You need to do the following:

    1. Implement the IValidatableObject interface
    2. Implement the IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    3. Implement your logic to determine that each string has less than 100 characters

    Here’s the code

    public class YourModel : IValidatableObject
    {
        public List<KeyValuePair<int, string>> Authors { get; set; }
    
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            foreach(KeyValuePair<int, string> myKvp in Authors)
            {
                if(myKvp.Value.Length >= 100)
                {
                    yield return new ValidationResult("Must be less than 100 characters");
                }
            }
        }
    }
    

    That way you can do a call to if(Model.IsValid) in your controller action, and return any errors that are reported. If your KeyValuePair entries are referring to a specific entity you can even do something like:

    yield return new ValidationResult("Must be less than 100 characters", new string[] { myKvp.Key.ToString() });
    

    You’d need to tailor it to fit the ID of the attribute on your page. This way, the error message could be specific to an input on your page.

    I override IValidatableObject in many places as there’s many cases where I do validation that’s dependent on the state of my object. Your case is a bit different, but it’s certainly do-able as can be seen from the above example. (All that’s off the top of my head, however, so may not be perfect!)

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

Sidebar

Related Questions

I have the following view model: Public Class MyViewModel Public Property SelectedIDs As List(Of
I have a class which must implement the following property public ICollection<IType> Items {
I have the following model property: [DataType(DataType.EmailAddress)] public string EmailAddress { get; set; }
I have the following model Public Class BaseViewModel Public Property Id as integer Public
I have the following model defined in .NET: Public Class Request <Key()> Public Property
I have following property in my Model Metadata class: [Required(ErrorMessage = Spent On is
I have the following property in the MVC Model: [Range(0, double.MaxValue, ErrorMessage = The
I have the following classes in my ActiveRecord model: def Property < ActiveRecord::Base #
i have the following property declaration Public Property IsAreaSelected() As Integer Get Return If(ViewState(IsAreaSelected)
I have the following property public MyType MyProperty {get;set;} I want to change 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.