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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:19:53+00:00 2026-06-02T10:19:53+00:00

While working through the MVC 2 NerdDinner tutorial I see that some business logic

  • 0

While working through the MVC 2 NerdDinner tutorial I see that some business logic (required fields, maximum lengths, etc.) can be added to individual properties on the model.

How do you add more complex validation rules? For example, requiring exactly one of two properties to be populated?

Example [QuantumMechanics/Models/Particle.cs]:

namespace QuantumMechanics.Models
{
   [MetadataType(typeof(Particle_Validation))]
   public partial class Particle {
   }
   public class Particle_Validation
   {
       // Mass is required; easy enough.
       [Required(ErrorMessage="Mass is required.")]
       public Mass double {get; set; }

       // How do I require exactly one or the other?
       public Position double {get; set; }
       public Momentum double {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-02T10:19:54+00:00Added an answer on June 2, 2026 at 10:19 am

    Thought I’d post the solution I went with. Like Spencerooni said, there is not an elegant mechanism to add complex validation to the model itself, but this seems to work. It leverages the DefaultModelBinder interface’s OnModelUpdated method override to invalidate the underlying model anytime it is updated.

    Note that DataAnnotations on a Model’s properties are only invoked when a bound field is posted back, which means validation will pass on a form without a field for Mass at all.

    Global.asax

    ModelBinders.Binders[typeof(Particle)] = new ParticleModelBinder();
    

    Models/ParticleModelBinder.cs

    public class ParticleModelBinder : DefaultModelBinder
    {
        protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            base.OnModelUpdated(controllerContext, bindingContext);
    
            var particle = (Particle)bindingContext.Model;
    
            if (<Test for business rule violation here>)
            {
                var modelState = bindingContext.ModelState;
                // Message to appear in validation summary.
                modelState.AddModelError("","Please enter position OR momentum.");
                // Messages also appear in summary, but highlight the bound controls too.
                modelState.AddModelError(bindingContext.ModelName + ".Position",
                    "Please enter position (or momemtum).");
                modelState.AddModelError(bindingContext.ModelName + ".Momentum",
                    "Please enter momentum (or position).");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working through Pro ASP.NET MVC book and I got the following code snippet that
Working through several layers of an MVC architecture designed program, I find that I
I am working through some Looping exercises, While statements in particular. Here are the
While working my way through the Android tutorials, I came across something I don't
While working in ASP.NET MVC, I often find myself defining a basic ViewModel which
While working through Real World Haskell, I tried to complete the palindrome exercise using
I am working through part4 of Symfony2 , and while updating the controller and
I came across this code while working through code-analysis warnings on our code base.
I'm having a frustrating time with Rspec while working through the Ruby on Rails
I am working on a ASP.NET MVC web site that has multiple submit buttons.

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.