我需要帮助使用剃刀视图填充 MVC 应用程序中的状态下拉列表:
在我的模型中,我有一个状态属性:
public SelectList State
{
get { return new SelectList(StateDictionary, "Value", "Key"); }
}
这是状态字典的缩短版本
public static readonly IDictionary<string, string> StateDictionary = new
Dictionary<string, string> {
{"ALABAMA", "AL"},
{"ALASKA", "AK"},
{"AMERICAN SAMOA", "AS"},
{"ARIZONA ", "AZ"},
{"ARKANSAS", "AR"},
{"CALIFORNIA ", "CA"},
{"COLORADO ", "CO"},
{"CONNECTICUT", "CT"},
{"DELAWARE", "DE"},
{"DISTRICT OF COLUMBIA", "DC"},
{"FEDERATED STATES OF MICRONESIA", "FM"},
{"FLORIDA", "FL"},
{"GEORGIA", "GA"},
};
在我的 Razor 视图中,我有:
<div class="form-group">
@Html.LabelFor(m => m.State,new{@class="col-lg-2 control-label"})
@Html.DropDownListFor(m => m.State,Model.State,new {@class="form-control" })
</div>
当我尝试访问我的视图时,我不断收到“未设置对象引用”错误
如何从此模型填充下拉列表?