Results 1 to 3 of 3
Thread: Odd MVC Validation Behaviour
Jun 3, 2009, 21:02 #1
Odd MVC Validation Behaviour
Ok, consider the following aspx code:
<%= Html.TextBox("Name", Model.Name) %>
<%= Html.ValidationMessage("Name", "*") %>
<%= Html.TextBox("Value", Model.Value) %>
<%= Html.ValidationMessage("Value", "*") %>
This is in a strongly typed Edit view. On initial display, things go smooth, as the record has values in both Name and Value. I then edit the value using the form, clearing the Value. field, and clicking save. Now..I've stepped through everything, and it binds fine and the validator does its job. However, on redisplay, it bugs out on the highlighted code. The debugged give an 'instance not set' message, but that's bogus. The line above it, where it reads out Model.Name works fine. In addition, when I inspect Model.Value, it says the value is "", which is what I would expect, since I submitted a blank value.
So what's up?
Jun 4, 2009, 05:36 #2
- Join Date
- May 2003
- Washington, DC
- 4 Post(s)
- 0 Thread(s)
There is lots of reflection and such going on, and Value is a pretty common string so perhaps the default binder is getting confused. Post the execption and the stack trace.
Jun 4, 2009, 05:52 #3
Hm. I looked into this more last night, and found that if I added a call to SetModelValue after my call to AddModelError, in my validator, and now things worked ok. I'm just not sure if that's where I should be setting it though. I'm not using the default binder. I cleared the collection and added custom binders for all my models. My binders pretty much set everything in a model (except ID) from the posted form values.
Is there some form of value shadowing going on in ViewData.ModelState? Because the Model HAS the right values. It's just without the call to SetModelValue in the validator, then client validation things couldn't read the values.