SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)

    Question Odd MVC Validation Behaviour

    Ok, consider the following aspx code:

    <p>
    <labelfor="Name">Name:</label>
    <%= Html.TextBox("Name", Model.Name) %>
    <%
    = Html.ValidationMessage("Name", "*") %>
    </p>
    <p>
    <labelfor="Value">Value:</label>
    <%= Html.TextBox("Value", Model.Value) %>
    <%
    = Html.ValidationMessage("Value", "*") %>
    </p>

    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?

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    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.

  3. #3
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •