MVC POST does not return a modified view model

I have a standard editing script with GET and POST, the form has a Save button and a Search button that allows the user to search for a zip code that fills the address and returns it to the form fields. The Search button returns to the Edit Controller method.

The following code is not real, but demonstrates my problem ...

[AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int CustomerId, string LookupButton) { Customer customer = new Customer(); UpdateModel(customer); //customer.County = "Hello world!"; return View(customer); ... } 

This code works as expected, it simply returns the existing form data, however, when I uncomment the line that manually changes the county field, these changes do not appear on the form. It left me because in uniform

 <%= ViewData.Eval("County") %> 

"Hello world!" will return. but

 <%= Html.TextBox("County") %> 

retains its former value!

 <input id="County" name="County" type="text" value="" /> 

The client is an EF4 class.

Any help is greatly appreciated.

+4
source share
1 answer

This is because the Html.TextBox first looks at the published query values, and then in the model that you are updating in your controller. In the values ​​of the placed queries, it finds the old value.

+4
source

Source: https://habr.com/ru/post/1299638/


All Articles