title: ModelState reference is different when accessed from another class
body: <p>I have a Controller class called <strong>ServiceController</strong> whose ModelState is being passed as a reference into an instance of another validation class called <strong>RequestValidator</strong>. When ServiceController receives a request, its RequestValidator class instance checks to see if the ModelState is valid or not. Problem is, for an erroneous input, the <code>IsValid</code> value for the ServiceController's own ModelState returns <code>false</code> (which is expected), but the <code>IsValid</code> value for Validator's ModelState is <code>true</code>.</p>
  <p>Here's some code on how the two classes are setup:</p>
  <pre><code>[Authorize]
  [ApiController]
  public class ServiceController : ControllerBase
  {
      private readonly RequestValidator validator;
  
      //This is the ServiceController's contructor:
      public ServiceController(RequestValidator validator)
      {
          //A validator instance is passed in, which is used by this ServiceController instance
          this.validator = validator;
          
          //The validator's ModelState is set to point to the ModelState of the ServiceController
          this.validator.ModelState = this.ModelState; // This is the ONLY point in my ServiceController code where validator.ModelState is written to
      }
  
      [HttpPost]
      [Route(&quot;v0.1/users&quot;)]
      public async Task&lt;ActionResult&lt;User&gt;&gt; ProvisionUser(
          [FromBody] UserProvisioningRequest userRequest)
      {
          //But when an erroneous request comes, both ModelStates give different IsValid value:
          
          //The following two variables should have same value, but they don't at runtime.
          bool thisModelStateValidation = this.ModelState.IsValid;
          bool validatorModelStateValidation = this.validator.ModelState.IsValid;
          
          //The hashcodes of both ModelStates are also different
          int thisModelStateHashCode = this.ModelState.GetHashCode();
          int validatorModelStateHashCode = this.requestPayloadValidator.ModelState.GetHashCode();
  
          this.validator.ValidateRequest(userRequest);
      }
  }
  
  
  public class RequestValidator
  {
      public ModelStateDictionary ModelState { get; set; }
  
      public void ValidateRequest(UserProvisioningRequest userRequest)
      {
          if (!this.ModelState.IsValid) // ---&gt; At this point, IsValid is true, whereas it should be false
          {
              
          }
      }
  }
  </code></pre>
  <p>As mentioned in a code comment above, even the hash codes of both ModelStates are different. But I don't understand why this is so because the value of <code>this.validator.ModelState</code> is always written to ONCE. The only way this can be explained is if <code>this.ModelState</code> gets reset somehow, but I don't see why that would happen.</p>