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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:30:31+00:00 2026-06-12T11:30:31+00:00

I am trying to validate a form using ModelState but my ModelState is always

  • 0

I am trying to validate a form using ModelState but my ModelState is always showing as valid. eg: When I am trying to save a 2 Person formModel each with the same SSN, the ModelState is returning valid. I am using IValidatableObject to validate my formmodel. Any ideas what I might be going wrong? I am using .Net 4.0 with MVC 3.

public JsonResult LoadOccupantsDetailedInformation()
{
    //Load the personsFormModel with data
    return new JsonpResult(personsFormModel, JsonRequestBehavior.AllowGet);
}

[HttpPost]
public ActionResult SaveOccupantsDetailedInformation(
PersonsFormModel personsFormModel)
    {
//This line is always returning true even if I pass 2 persons with the same ssn
        if (ModelState.IsValid == false)
        {
            var errorList = ModelState.ToDictionary(
                      kvp => kvp.Key,
                      kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
                );
            return Json(new { Errors = errorList });
        }
        //Save the data in personsFormModel to database
        return Json(new JsonResultViewModel { Success = true });
    }


public partial class PersonsFormModel : List<PersonFormModel>, IValidatableObject
{
    public IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> Validate(
    ValidationContext validationContext)
    {
        var validationResults
            = new List<System.ComponentModel.DataAnnotations.ValidationResult>();
        if (this.SSNs.Count() > this.SSNs.Distinct().Count())
        {
            validationResults.Add(new System.ComponentModel.DataAnnotations.ValidationResult(
            "All the persons in the household should have a unique SSN\\ITIN number",
                new[] { "SSN" }));
        }
        return validationResults;
    }
    private IEnumerable<string> SSNs
    {
        get
        {
            return this.Select(element => element.SSN);
        }
    }
}
    public class PrequalifyOccupantListPersonDetailedFormModel
{
    [Required(ErrorMessage = "SSN is required")]
    public string SSN { 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-12T11:30:32+00:00Added an answer on June 12, 2026 at 11:30 am

    This is how I fixed the issue that I was having:

    ***Controller:***
    public JsonResult LoadOccupantsDetailedInformation()
    {
        //Load the personsFormModel with data
        return new JsonpResult(personsFormModel, JsonRequestBehavior.AllowGet);
    }
    
    [HttpPost]
    public ActionResult SaveOccupantsDetailedInformation(
    PersonsFormModel personsFormModel)
        {
    //This line is always returning true even if I pass 2 persons with the same ssn
            if (ModelState.IsValid == false)
            {
                var errorList = ModelState.ToDictionary(
                          kvp => kvp.Key,
                          kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
                    );
                return Json(new { Errors = errorList });
            }
            //Save the data in personsFormModel to database
            return Json(new JsonResultViewModel { Success = true });
        }
    
    ***Model:***
    public class PersonsFormModel : IValidatableObject
    {
        public PersonsFormModel()
        {
            this.Instance = new List<PersonFormModel>();
        }
    
        public List<PrequalifyOccupantListPersonFormModel> Instance { get; set; }
    
        public void Add(PersonFormModel personFormModel)
        {
            Instance.Add(personFormModel);
        }
    
        public IEnumerable<System.ComponentModel.DataAnnotations.ValidationResult> Validate(
        ValidationContext validationContext)
        {
            var validationResults
                = new List<System.ComponentModel.DataAnnotations.ValidationResult>();
            if (this.SSNs.Count() > this.SSNs.Distinct().Count())
            {
                validationResults.Add(new System.ComponentModel.DataAnnotations.ValidationResult(
                "All the persons in the household should have a unique SSN\\ITIN number",
                    new[] { "SSN" }));
            }
            return validationResults;
        }
        private IEnumerable<string> SSNs
        {
            get
            {
                return Instance.Select(element => element.SSN);
            }
        }
    }
    
    public class PersonFormModel
    {
        [Required(ErrorMessage = "SSN is required")]
        public string SSN { get; set; }
    }
    
    View: 
    
    // Note, I am passing the "Instance" here. 
    
     var postData = JSON.stringify({ Instance: list });
    
                RealPage.DataManager.POST({
                    url: "/Approval/SaveOccupantsDetailedInformation"
                    , data: postData
                    , success: function (data) {
                        if (data && data.Success) {
    
                        } 
                    }
                }); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to validate a form using ModelState generally the same way as
I'm trying to validate my php form using exception, but somehow it doesn't work.
I am trying to validate some form fields but it always errors out on
I'm trying to create a registration form and validate input fields using javascript, but
I am trying to validate my form using jquery but I cannot get it
Friends, I am trying to validate my html form using an external javascript but
I'm trying to validate a simple form in JSP with Spring and Hibernate using
I am trying to using a jquery.validate.unotrusive.js plugin to dynamically created form fields like:
I'm trying to validate a form using Data Annotation. It seems great for string
I'm trying to validate a form using an AJAX call to check available inventory.

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.