Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems back button and resubmitting the form
    text
    copied!<p>I am having problems when the back button is used and a form is resubmitted, the value on the model that is bound to the drop down list is not retained on the second submission. </p> <p>In real life, this code is a report generator - the form collects parameters from the user and then posts it to a results view where the data is displayed. Obviously this is a fairly complex bit of code to post, but below I have some simple code exhibiting the same bug:</p> <p>I have a model like so:</p> <pre><code>public class HomeModel { public string SomeText { get; set; } public long SelectedItemId { get; set; } public IEnumerable&lt;SelectListItem&gt; AvailableItems { get; set; } } </code></pre> <p>A form that submits an instance of the model like so:</p> <pre><code>@using(Html.BeginForm("Result", "Home", FormMethod.Get)) { @Html.EditorFor(model =&gt; model.SomeText) &lt;br /&gt; @Html.DropDownListFor(model =&gt; model.SelectedItemId, Model.AvailableItems) &lt;input type="submit" value="submit" /&gt; } </code></pre> <p>And a view that shows the details, like so</p> <pre><code>You Typed: @Html.DisplayFor(model =&gt; model.SomeText) &lt;br /&gt; You Selected: @Html.DisplayFor(model =&gt; model.SelectedItemId) </code></pre> <p>And a controller to manage it all:</p> <pre><code> public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { HomeModel model = new HomeModel(); List&lt;SelectListItem&gt; items = new List&lt;SelectListItem&gt;(); for (int i = 0; i &lt; 10; i++) items.Add(new SelectListItem() { Value = i.ToString(), Text = i.ToString() }); model.AvailableItems = items; return View(model); } public ActionResult Result(HomeModel model) { return View(model); } } </code></pre> <p>I fill in the form, press submit. I am redirected to the Results view and all is well.</p> <p>Then I press back in the browser, and I am shown the Index view again.</p> <p>Then I submit the form again and things turn sour. The model is not being repopulated with the selected value from my list, despite the list appearing to show the previously selected value in the browser.</p> <p>The string property is being updated correctly, and this is true in the real world code that I have displaying this behaviour.</p> <p>This only happens in IE, I am using version 9. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload