Justin Helgerson
Justin Helgerson

Reputation: 25551

Firefox is caching hidden inputs even with autocomplete turned off

I have a form with the autocomplete attribute set to off.

Inside this form, there is a hidden input element (generated using ASP.NET MVC's Html.HiddenFor() method, but, that should be irrelevant):

<input type="hidden" value="0" name="Status" id="Status" data-val-required="The Status field is required." data-val-number="The field Status must be a number." data-val="true">

When the form is submitted, this value is incremented by one and the model is returned to the view. If I write the status value to the page, I can see that the value was correctly incremented.

However, this hidden input field is always cached. It's never getting the correct value. I tried setting the autocomplete attribute directly on the input element, but without success.

How can I get this hidden field to get the correct value? I'd prefer not to use any Javascript.

Edit: Supplying more code...

Controller

//This code is being executed, and the status is being incremented.
shippingOrder.Status = (shippingOrder.Status != (int)OrderStatus.Closed) ? shippingOrder.Status + 1 : shippingOrder.Status;

View

@using (Html.BeginForm("Number", "Order", FormMethod.Post, new { id = "orderSummary", autocomplete = "off" })) {
    ...
    @Html.HiddenFor(m => m.Status)
}

Upvotes: 4

Views: 1713

Answers (1)

Nope
Nope

Reputation: 22339

According to this post here html helpers such as HiddenFor will always first use the posted value and after that the value in the model.

As you said, when writing the value to the page you can see it incremented, yet the helper is using the previously posted value, which is the intended behaviour.

The link does suggest the use of ModelState.Remove("Status") or ModelState.Clear() before assigning the new value.

It also suggest that another option could be not using a HiddenFor helper but instead to build the hidden field yourself. Similar to this:

<input type="hidden" name="Status" value="@Model.Status" />

Either way it looks like your problem is based on similar circumstances.

Upvotes: 3

Related Questions