我有一個簡單的HTML表單,其中dropdwonListFor綁定到顏色,它下面的文本框,並提交按鈕提交表單並保存顏色。MVC3 HTML助手不會更新DropdownListFor提交表單
當我從下拉列表中選擇一種顏色時,如果用戶單擊提交表單,它將更改其下方文本框的值。它會返回到控制器,並保存texebox中的顏色,並將視圖(模型)作爲操作結果返回,但問題是dropdownlistfor不會使用文本框的值更新,無論文本框中的值是下拉列表與否。
順便說一句,你可以自己測試 任何人都可以幫忙嗎?
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" />
}
我們如何在沒有看到您的代碼的情況下自行測試? –
創建一個MVC項目,放一個dropdownlistFor和TextBoxFor和一個提交按鈕,並在控制器更改屬性綁定到下拉列表中,你會看到它保持舊值不是更新的 –
是不是更好,你添加代碼你的問題?我們免費爲您提供幫助,這意味着我們大多數人都不願意嘗試重現您的錯誤,而不是改善您的問題。 – jgauffin