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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:31:32+00:00 2026-06-04T11:31:32+00:00

I have a custom data annotation validator to validate the date of birth to

  • 0

I have a custom data annotation validator to validate the date of birth to be between 150 years from today.

Here is my custom data annotation:

public class DateOfBirthRange : RangeAttribute
{
    public DateOfBirthRange()
        : base(typeof(DateTime), DateTime.Now.AddYears(-150).ToShortDateString(), DateTime.Now.ToShortDateString()) { }
}

Using it like this:

[Required(ErrorMessage = "BirthDate is required.")]
[DisplayName("Birth Date")]
[DateOfBirthRange(ErrorMessage = "BirthDate must be between {1:M/d/yyyy} and {2:M/d/yyyy}")]
public DateTime BirthDate { get; set; }

This is a very strange problem because the error that I’m getting back has nothing to do with the data annotation. Its causing an error in my view here:

<%: Html.DropDownListFor(m => m.JuniorSenior, (IEnumerable<SelectListItem>)ViewData["seniority"], new { @class = "input-small" })%>

ERROR: The ViewData item that has the key 'JuniorSenior' is of type 'System.String' but must be of type 'IEnumerable'.

That error is strange because that part of the code is working perfectly fine. What also makes this strange is that this error only occurs when the date entered is before 150 years from today. So that error only occurs when the date of birth validation fails. As I was debugging, I noticed that once I remove the custom data annotation, everything works fine and no error is encountered.

So that leads me to assume the problem is in my data annotation.

Here is my controller code in case you want to see what I’m trying to do.

[HttpPost]
        public ActionResult Save(PatientModel patModel, FormCollection values)
        {
            // remove white space form text box fields
            patModel.FirstName = values["FirstName"].Trim();
            patModel.LastName = values["LastName"].Trim();
            patModel.Initials = values["Initials"].Trim();
            patModel.StreetAddress1 = values["StreetAddress1"].Trim();
            patModel.StreetAddress2 = values["StreetAddress2"].Trim();
            patModel.PostalCode = values["PostalCode"].Trim();

            if (ModelState.IsValid)
            {
                try
                {
                    // Pull the long form of the gender into the model.
                    if (!String.IsNullOrEmpty(values["genders"]))
                    {
                        patModel.Gender = values["genders"];
                    }
                    // Profile is valid, save it.
                    if (_Service.SaveProfile(Session["username"].ToString(), Session["password"].ToString(), patModel))
                        ViewData["SaveProfile"] = true;
                    else
                        ViewData["SaveProfile"] = false;
                }
                catch (Exception ex)
                {
                    logger.Error("Function Name: Save  Message: " + ex.Message + "");
                }
            }
            IntializeSelectLists(patModel);
            if (patModel.Title == "Select")
                patModel.Title = "";
            if (patModel.JuniorSenior == "Select")
                patModel.JuniorSenior = "";
            return View("Index", patModel);
        }

My IntializeSelectLists function:

public void IntializeSelectLists(PatientModel pm)
        {
            seniority = new[] { "Select", "Jr.", "Sr." };
            List<SelectListItem> JuniorSenior = new List<SelectListItem>();
            foreach (string item in seniority)
            {
                SelectListItem alb = new SelectListItem { Text = item, Value = item };
                JuniorSenior.Add(alb);
            }
            ViewData["seniority"] = JuniorSenior;
        }

Any help would be appreciated.

  • 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-04T11:31:34+00:00Added an answer on June 4, 2026 at 11:31 am

    After bumping my head around I final found what the problem was.

    In my IntializeSelectLists() I was populating the genders list right before the JuniorSenior list. As it turns out, the pm.Gender was returning null and therefore causing an exception.

    The reason it was crashing at m.JuniorSenior was because I was displaying that before the m.Gender. So m.JuniorSenior was always null because the code didn’t get to the point where it populated the m.JuniorSenior.

    patModel.Gender = values["genders"].Trim();
    

    I solved the problem by adding this in my Save().

    Thanks to everyone that took the time to help me 🙂

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

Sidebar

Related Questions

How do i select elements that have my custom data attribute data-validate with jQuery
I have a custom list view that is getting data from server and changing
I have an interface in C# that helps retrieving of data from a custom
I have mastered creating custom data types and adding fields with CCK. Then I
I am trying to have a tree display custom data that looks like this.
The task: I am trying to create a custom data type and have it
I have a custom image file where the first block of data is ASCII
We have created a custom dataset and populating it with some data. Before adding
I have a data object -- a custom class called Notification -- that exposes
I have a wpf app that needs to communicate(exchange data) with a custom designed

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.