views:

214

answers:

1

I am using an AJAX form post to send some form fields to the server, where the data will be updated in the database, and is massaged in the process, so the data may change a bit on the server side. The response of this controller's action is a partial that updates the form field's HTML so that the values of those fields include the new massaged values.

The trouble is, apparently some MVC .js must be executing on the returned partial's HTML to change the value of the text fields back to their originally posted values, so that the massaged values never show up.

I can see how this might be useful in some scenarios, but it's defeating my scenario. How do I suppress this behavior?

EDIT I've discovered that if in my partial I replace this:

<%= Html.TextBox("FirstName", Model.FirstName) %>

with this:

<input name="FirstName" value="<%= Html.Encode(Model.FirstName) %>" />

that the value in the form field updates as I would expect. So it seems there's some magical side-effects of Html.TextBox that I don't yet understand.

+1  A: 

I don't think there's any random javascript being executed. MVC doesn't rely on javascript, and certainly wouldn't be injecting it into your pages.

More likely the issue is that your ModelStateDictionary is holding the original values. Values that exist simply in the form post but that are not applied to the model are not going to show up. See this thread for some helpful pointers.

womp
Well, I am including `MicrosoftAjax.js` and `MicrosoftMvcAjax.js` in the web page so that the ajax stuff works, so it's not like magic random javascript is injected. Since this .js is the code I'm using to perform the ajax form POST, it has every opportunity to do what I was suspecting it was doing. That said, I'll check out the `ModelStateDictionary` as you suggested and see what I find.
Andrew Arnott
I very likely don't understand all the interactions here. But I render the partial view with a strongly-typed model object and I still see only the old values in the web browser. I've initialized the model to be totally empty, or to have new values, and either way, the old values (the values the user originally POSTed) show up in the refreshed partial, despite the fact that the partial's HTML itself does have the correct (new) form field values.
Andrew Arnott
BTW, if I craft the partial view so that it changes the name of one of the form fields (from FirstName to FirstName1), the *new* value will be displayed in it rather than the old one. This really suggests to me that the browser javascript is changing the values on recognized form fields.
Andrew Arnott
As 'womp' said, the ModelStateDictionary is used to preserve the values of what the user typed into the browser. This is make scenarios such as the user typed in "dog" for the "Age" field. You don't want the value cleared out - you want "dog" preserved, and to just show the user an error. What you can do is clear out the model state dictionary (or at least the fields you care about).
Eilon
And this is definitely *not* the JavaScript changing any values. The JavaScript code used by ASP.NET MVC does no such thing. There is, however, a bug in Mozilla Firefox where certain input tags get reset to their original values even aftering being updated via the DOM. To confirm is that's what you're seeing, try to run the application in Internet Explorer.
Eilon
Fascinating... so the `ModelState` property *overrides* the data in a model that I provide to a partial view, eh? (see... I'm pretty new to ASP.NET MVC). So by selectively deleting key=value pairs in that dictionary I force the view to use the model that I gave it for those values. That seems to work. Thanks a bunch, Eilon and Womp!
Andrew Arnott