正如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