正如womp說,瀏覽器將只能提交一個下拉列表中選擇的值。這很容易被默認的模型綁定器綁定,見下文。
如果您未在客戶端上編輯PossibleValues列表,則無需將其提交回去。如果您需要重新填充列表,請在您的發佈操作中使用您最初填寫Dictionary的相同方法在服務器端執行此操作。
例如,在你頁面:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<ViewModel1>" %>
<!-- some html here -->
<%= Html.DropDownListFor(x => x.SelectedKey, new SelectList(Model.PossibleValues, "key", "value"))%>
在你的控制器
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Edit() {
var model = new ViewModel1 {
PossibleValues = GetDictionary() //populate your Dictionary here
};
return View(model);
}
[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Edit(ViewModel1 model) { //default model binding
model.PossibleValues = GetDictionary(); //repopulate your Dictionary here
return View(model);
}
凡GetDictionary()是返回你的填充Dictionary對象的方法。
See this similar question for more details
您使用的是ASP.NET MVC版本1還是2? – 2010-01-30 11:23:01
我正在使用版本2 – jameszhao00 2010-01-30 20:15:33