Mark
Mark

Reputation: 41

MVC POST not returning modifed view model

I have a standard Edit scenario with GET and POST, the form has a Save button and a Lookup button that allows the user to lookup a postcode, which fills out the address and returns it in the form fields. The Lookup button posts back to the Edit controller method.

The following isn't real code 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 does as expected, just returns the existing form data, however when I uncomment the line that manually changes the County field, those changes don't appear on the form. This has thrown me, because in the form

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

will return "Hello world!" but

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

still retains the old value!

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

Customer is an EF4 class.

Any help much appreciated.

Upvotes: 4

Views: 1385

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

That's because the Html.TextBox first looks in the posted request values and then in the model that you update in your controller. In the posted request values it finds the old value.

Upvotes: 4

Related Questions