我正在学习MVC3,我希望我的下拉列表使用colors
作为数据。我该怎么做?如何使用此代码执行@ Html.DropDownListFor?
我知道我可以用@Html.DropDownList("colors")
做到这一点,但我想知道如何用@Html.DropDownListFor(....)
做到这一点?我有点难住,任何帮助和解释,将不胜感激。
为了方便起见,我把它放在一个页面中,所以这里不是真实世界的应用程序。
@functions {
private class Colors
{
public int ColorsId { get; set; }
public string ColorsName { get; set; }
}
}
@{
var list = new List<Colors>()
{
new Colors() {ColorsId = 1, ColorsName = "Red"},
new Colors() {ColorsId = 2, ColorsName = "Blue"},
new Colors() {ColorsId = 3, ColorsName = "White"}
};
var colors = new SelectList(list, "ColorsId", "ColorsName", 3);
}
@Html.DropDownListFor(???)
检查我的回答这个线程。它应该非常相似。 http://stackoverflow.com/questions/5097290/html-listboxfor-error-problem-asp-mvc-3/5176057#5176057 – 2012-04-10 12:30:19
@AllenWang谢谢。绝对是一个很好的答案。提出了一个。 – 2012-04-10 17:15:48