【发布时间】:2010-08-13 11:45:35
【问题描述】:
又是一个新手问题。 ;-)
我正在设置我的视图模型基于 this very helpfull post:
public class My_IndexModel
{
...
public IEnumerable<SelectListItem> My_DropDownList { get; set; }
...
}
请注意我使用的是
IEnumerable<SelectListItem>
能够设置选定的属性。
下拉列表值在控制器中设置:
public ActionResult Index()
{
...
var DropDownList_Values = from value in My_DB.Value
select new SelectListItem
{
Selected = (value.IsDefault == 1),
Text = value.Value1,
Value = value.Value1
};
...
var viewModel = new My_IndexModel
{ ...
My_DropDownList = DropDownList_Values.ToList(),
...
}
...
return View(viewModel);
}
意味着我的 ViewData 模型包含(在我的情况下)一个下拉列表。
下拉列表也显示在我的网站 (*.aspx) 中,如下所示:
<%: Html.DropDownList("MyDropDownListField",
new SelectList(Model.My_DropDownList as IEnumerable,
"Value",
"Text",
Model.My_DropDownList
)
)%>
到目前为止没问题。
在网站 (*.aspx) 上,我有一个简单的“提交”按钮,它返回所有数据。 我在这里获取提交事件:
[HttpPost]
public ActionResult Index(My_IndexModel model)
{
if (ModelState.IsValid)
{
... model.My_DropDownList ...
}
}
但是 DropDownList 是空的!
必须做什么才能获得选定的下拉列表值 在 [HttpPost] 方法中?
【问题讨论】:
-
我衷心建议您至少阅读一份官方的 ASP.NET MVC 教程——最好是其中包含 DropDownList 的教程。 :) 他们非常擅长让您快速而牢固地掌握视图、操作以及在两者之间传递数据。 asp.net/mvc/tutorials/mvc-music-store-part-2
-
谢谢。好的。诚然,看一下教程会有所帮助......抱歉发布这样的问题是如此愚蠢。
标签: asp.net-mvc-2