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.
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.
By the way you can test it urself Can anybody help please ?
Model.cs
public class TestModel {
public String Color { get; set; }
}
Controller.cs
public ActionResult Index() {
var model = new TestModel();
model.Color="Blue";
ViewData["Colors"]=new List<SelectListItem>() { 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<SelectListItem>() { new SelectListItem() { Text = "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "Red" } };
return View(model);
}
Index.cs
@using (Html.BeginForm()) {
@Html.DropDownListFor(m => m.Color, ViewData["Colors"], new { @class = "w200" })
<input type="submit" />
}