【发布时间】:2013-12-31 17:10:51
【问题描述】:
由于我以前从未在 MVC 中使用过下拉列表,因此在创建下拉列表时遇到了问题。 我已经查过了,但我不明白。我想在 MVC3 中为“性别”创建一个基本的下拉列表。
到目前为止,我已经能够创建这个。
类:
public Dictionary<int, string> Gender {get;set; }
public StudentInformation()
{
Gender = new Dictionary<int, string>()
{
{ 0, "Male"},
{ 1, "Female"},
};
}
查看:
@Html.DropDownListFor(model => model.Gender.Keys,
new SelectList(
Model.Gender,
"Key",
"Value"))
但它会抛出异常“对象引用未设置为对象的实例”。
【问题讨论】:
-
请展示你的努力。你试过什么?一些代码...
-
现在检查我已经编辑了它。
-
你得到一个 NullReferenceException,这对你来说应该很容易调试。模型是否为空?
标签: asp.net asp.net-mvc asp.net-mvc-3