tags:

views:

41

answers:

1

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.

A: 

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.

Darin Dimitrov
but if I do <%= Html.TextBox("County", Model.County) %>then the result is the same, it still pulls the values from Request.Form and I can't push values in there because it's read-only.
Mark
It seems the data is coming from the ModelState. If I issue ViewData.ModelState.Clear();before returning the View, then it works as expected.
Mark
I was having a similar issue. We have a "Save and Continue" button in the application, which returns a new model, but reuses one value from the posted model. It was, however, returning all values for some reason. Clearing the ModelState did the trick!
Pawel Krakowiak