Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 HTML helper doesn't update DropdownListFor on Submit the form
    text
    copied!<p>I have a simple HTML form with dropdwonListFor bound to colors, a textBox below it and submit button to submit the form and save the color.</p> <p>When I select a color from the dropdownlist, it will change the value of the textbox below it, if the user clicks the submit form. it goes back to the controller and I save the color from the texebox and return view(model) as an action result, but the problem that the dropdownlistfor doesn't get updated with the value of the textbox whether the value in the textbox within the dropdownlist or not.</p> <p>By the way you can test it urself Can anybody help please ?</p> <p>Model.cs</p> <pre><code>public class TestModel { public String Color { get; set; } } </code></pre> <p>Controller.cs</p> <pre><code>public ActionResult Index() { var model = new TestModel(); model.Color="Blue"; ViewData["Colors"]=new List&lt;SelectListItem&gt;() { new SelectListItem() { Text = "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "Red" } }; return View(model); } [HttpPost] public ActionResult Index(TestModel model) { model.Color="Red"; ViewData["Colors"]=new List&lt;SelectListItem&gt;() { new SelectListItem() { Text = "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "Red" } }; return View(model); } </code></pre> <p>Index.cs</p> <pre><code>@using (Html.BeginForm()) { @Html.DropDownListFor(m =&gt; m.Color, ViewData["Colors"], new { @class = "w200" }) &lt;input type="submit" /&gt; </code></pre> <p>}</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