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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:45:11+00:00 2026-06-11T17:45:11+00:00

I’ve been struggling with refactoring how I’m doing Model validations for a while now.

  • 0

I’ve been struggling with refactoring how I’m doing Model validations for a while now. My applications are all ASP.NET MVC 2 (.Net 3.5 framework). I’ve read the related questions for this subject and they’re not exactly the same as what I’m asking.

Currently, for page validations, I generally stuck with a hodge-podge of if/else statements. The issue is that sometimes field B is validated if field A is a certain value, but field A has it’s own validations as well. In some cases there are roles that come into play and this may altar the validations as well depending on the role of the user (i.e. don’t validate fields the user with role x can’t modify).

My questions boil down to this:

1) How can I handle validations in an easier to read way? I’ve looked at DataAnnotations and they’re not enough. A data annotation handles validation for a field’s value, where as the validations that may need to be done depend on both the field’s value and the value’s of other properties on the object.

2) How can I integrate a validation framework with ASP.NET MVC 2?

The thing I truly hate about how I’m doing my validations now is that, when I add a model error, I am hard coding the key for that property. That’s pretty bad. Still, I’m not sure how to get the key for said property such that it matches the html field that was created by the html helper.

For example, in my view, I am using an editor template to display the fields for my customer object.

   <%= Html.EditorFor(m => m.CustomerInfo) %>

Part of the html generated creates the field for the FirstName:

<input id="CustomerInfo_FirstName" name="CustomerInfo.FirstName" style="width:330px;" type="text" value="John" />

In order for the framework to still highlight the field with the necessary CSS, I am forcing this validation like so:

//Ignore that this can be done with DataAnnotations. This is just a simple example.   
if (String.IsnullOrEmpty(customerInfo.FirstName) )
{
    ModelState.AddModelError("CustomerInfo.FirstName",
    "First Name is required.");
}

This is what I see as the major issue. I don’t have a good way to know the name of the field on the page. It seems to take the pattern of

 <objectname>.<propertyname>

, but I would really want something more reliable.

To get the object name, I think I could pull it out of the ModelBindingContext class, but I’m not sure how to get an instance of this from inside of the controller. For the property name, I could use somekind of lambda that would return the name of the property as a string.

To summarize, I am looking for: 1) some kind of validation framework that would allow complex validations 2) Some way to identify the key to use in the ModelState.AddModelError method such that MVC’s error styling still works.

  • 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-11T17:45:13+00:00Added an answer on June 11, 2026 at 5:45 pm

    You really are looking for FluentValidation

    However, I’m not sure if it supports MVC2. It supports for sure MVC3 and MVC4 though.

    Example code from its website:

    public class CustomerValidator: AbstractValidator<Customer> {
      public CustomerValidator() {
        RuleFor(customer => customer.Surname).NotEmpty();
        RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Please specify a first name");
        RuleFor(customer => customer.Company).NotNull();
        RuleFor(customer => customer.Discount).NotEqual(0).When(customer => customer.HasDiscount);
        RuleFor(customer => customer.Address).Length(20, 250);
        RuleFor(customer => customer.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode");
      }
    
      private bool BeAValidPostcode(string postcode) {
        // custom postcode validating logic goes here
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.