开发者

How to stop ASP.NET MVC ajax form POST responses from having their form fields reset?

开发者 https://www.devze.com 2022-12-15 06:53 出处:网络
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

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 M开发者_如何转开发VC .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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消