does TryValidateModel work against same instance of model

636 Views Asked by At

So, my data can be altared outside of my app (in a legacy system). Thus, when rendering the model on a GET, I perform a TryValidateModel to make sure nothing has changed since it was last stored that would cause my model to go 'bad'.

My model has a particular extended property / object graph (e.g. ModelInstance.Addresses.Generator.Address). It seems to load just fine in the first line of my Action method (below). If I put Debug.WriteLine statements, for example before and after ModelState.Clear.. or before and after TryValidateModel, they successfully test and detect this portion of my model.. it's not null. and the actual View razor executes and successfully expresses the associated HTML markup to the client - the page looks correct, and the 'Address' displays where it should.

However, during the TryValidateModel call.. the piece of validation logic that checks to make sure that the 'Address' property is not null... it fails. Again, before and after the TryValidteModel call, you can place your cursor in the intelliense/breakpoint and see the property in question.. it exists..

If I put a breakpoint within the IValidatableObject method that I am using.. then the property is magically null and thus fails validation..

Function Edit(ByVal id As Integer) As ActionResult
    'load from WCF or from web cache
    Me.modelInstance.LoadFromCacheOrService(id)

    'breakpoint here / intellisense / add-watch shows valid model
    'for Me.modelInstance.Addresses.Generator.Address

    ModelState.Clear()

    'breakpoint here / intellisense / add-watch shows valid model
    'for Me.modelInstance.Addresses.Generator.Address

    'but, inside the tryvalidate .. it says the model is invalid 
    '(and F11 stepping into validation logic indeed it seems to be invalid)
    Me.TryValidateModel(Me.modelInstance)
    If Not ModelState.IsValid Then
        ViewBag.Info = String.Format("Looks like there's a few things you ought to fix and re-save for this order")
    End If

    'but the view renders correctly.. the 'address' property is indeed 'there'
    'and investigating the model in Visual Studio debugger shows the property is not null
    Return View("Edit", modelInstance)

End Function

I've double-checked my model property getters/setters to make sure I didn't hide away some magical logic that I had since forgotten. I've triple checked to ensure the instance I'm loading is the same instance that I'm passing to TryValidateModel... but am so flummoxed that I wrote up this extended question here on SO in hopes someone else has encountered this issue.


I presume the next step is to grab a copy of the MVC source, step through the entire stack and figure out what else might be interacting with my model. It really feels like I'm dealing with 2 separate object graphs.

1

There are 1 best solutions below

0
On

Ok. I finally figured it out. Indeed, there was a separate model instance of the same type being invoked. And it all makes sense now...

I noticed the invalid ModelState's collection of errors were related to another object graph that I didn't immediately recall ... (much head scratching ensued)..

Then, I finally recalled that my 'Order' object featured a newly added property that allowed it to cross reference another Order.. we'll call the new property I added "CrossReferenceOrder" and it was of the same type as my main "Order" object.. which meant it also implemented IValidatableObject -

Once I figured out that property binding / validation was triggering this 'leaf' of the main object graph to be instantiated and validated, I was able to remedy things rather quickly.