2011-10-10 24 views
1

我有一個簡單的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" /> 

}

+0

我們如何在沒有看到您的代碼的情況下自行測試? –

+0

創建一個MVC項目,放一個dropdownlistFor和TextBoxFor和一個提交按鈕,並在控制器更改屬性綁定到下拉列表中,你會看到它保持舊值不是更新的 –

+0

是不是更好,你添加代碼你的問題?我們免費爲您提供幫助,這意味着我們大多數人都不願意嘗試重現您的錯誤,而不是改善您的問題。 – jgauffin

回答

0

好的傢伙,問題不在於你實現這個場景的方式,這裏的問題是ModelState。我發佈到操作並返回相同的視圖。第二次渲染視圖時,它將查看ModelState並使用這些值來填充控件。 所以我們只需要在返回視圖之前清除ModelState。

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" } }; 

     ***ModelState.Clear();*** 
     return View(model); 
} 

Index.cs

@using (Html.BeginForm()) { 
@Html.DropDownListFor(m => m.Color, ViewData["Colors"], new { @class = "w200" }) 
<input type="submit" /> 

}

Cheeeeeers

1

模型

public class TestModel { 
    public String Color { get; set; } 
    public SelectList Colors {get;set;} } 

控制器

public ActionResult Index() { 
     var model = new TestModel(); 
     model.Color="Blue"; 
     var colors =new List<SelectListItem>() { new SelectListItem() { Text = "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "red" } }; 
     model.Colors = new SelectList(colors,"Text","Value"); 

     return View(model); 
    } 

[HttpPost] public ActionResult Index(TestModel model) { 
     model.Color="Red"; 

     var colors =new List<SelectListItem>() { new SelectListItem() { Text = "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "red" } }; 
     model.Colors = new SelectList(colors,"Text","Value"); 

     return View(model); } 

查看

@using (Html.BeginForm()) { 
    <div> 
     @Html.DropDownListFor(m => m.Color, Model.Colors, new { @class = "w200" }) 
     <input type="submit" /> 
    </div> 
} 
+0

這是同樣的事情,我可以對它進行修飾並完成相同的操作,但這不是我的問題,HTML幫助程序未更新dropdownList的問題,請在你的機器上測試 –

+0

@Alaa Osta:你測試了我的改變嗎?它與你的代碼不一樣 – Gregoire

+0

我剛剛用你發佈的完全相同的代碼對它進行了測試,顏色不會從藍色變爲紅色。請自己檢查一下,回來和我一起這對我們來說太重要了 –

0

你需要在你的後行動的所有顏色。

也不要使用ViewData的,但添加的項目,以查看型號:

public class TestModel { 
    public String Color { get; set; } 
    IEnumerable<SelectListItem> AvailableColors {get;set;} 
} 

但由於視圖模型旨在用於抽象掉你的模型,你可以這樣做:

public class TestModel { 
    public TestModel(IEnumerable<string> colors) 
    { 
     AvailableColors = colors.Select(c => new SelectListItem{Text=c, Value = c}); 
    } 

    public String Color { get; set; } 
    IEnumerable<SelectListItem> AvailableColors {get;} 
} 

而且在你的控制器:

public ActionResult Index() { 
    var model = new TestModel(new string[]{"Red", "Green", "Blue"}); 
    model.Color="Blue"; 
    return View(model); 
} 
+0

它包含了,請自己測試 –

+0

不,它也不工作,請自己測試一下,我想知道如果我做錯了什麼或者我錯過了什麼,謝謝。 –

0

爲了使下拉列表變化選擇了什麼,您必須將與該文本框值相對應的選擇列表項的選定屬性設置爲true。事情是這樣的:(注:我沒有編譯和測試,這可能需要調整,以獲得它來編譯另外,我認爲,如果一個顏色列表中不應該被添加。)

[HttpPost] 
public ActionResult Index(TestModel model) { 
     model.Color="Red"; 
     var colors = new List<SelectListItem>() { new SelectListItem() { Text = "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "Red" } }; 

     SelectListItem selectedColor = colors.Where(c => c.Text == model.Color).FirstOrDefault(); 
     if (selectedColor != null) 
     { 
      selectedColor.Selected = true; 
     } 
     else 
     { 
      colors.Add(new SelectListItem() { Text = model.Color; Value = model.Color; Selected = true; }; 
     } 
     ViewData["Colors"] = colors; 
     return View(model); 
} 

編輯

做一些測試和挖掘後,看來你需要使用JavaScript來做到這一點在這個SO question解釋的那樣。另一種選擇是推出自己的幫手。

+0

對不起,說它也沒有工作,請自己測試以確認。感謝您的努力 –

+0

@AlaaOsta - 我很驚訝,它沒有奏效。但是我確實找到了答案(請參閱我的編輯)。 – Becuzz

+0

你需要清除ModalState的答案,因爲它仍然有舊視圖檢查我的答案 –