-1

我收到以下异常:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an    object.

Source Error: 
Line 34:                 <div>
Line 35:                     @Html.LabelFor(m => m.accountStatus)
Line 36: (HERE)              @Html.DropDownListFor(m => m.accountStatus, Model.accStat) 
Line 37:                 </div>
Line 38:         </fieldset>

以下是我正在使用的模型。我在我的模型中创建了一个 List 属性。我在我的 Html.DropDownListFor 助手中引用它。但我得到了例外。

public class OperatorModel
{
    [Required]
    [Display(Name = "Account ID")]
    public string accountID { get; set; }

    [Required]
    [Display(Name = "Account Name")]
    public string accountName { get; set; }

    [Required]
    [Display(Name = "Password")]
    public string password { get; set; }

    public List<SelectListItem> accStat
    {
        get
        {
            List<SelectListItem> lst = new List<SelectListItem>();
            lst.Add(new SelectListItem { Text="ACTIVE", Value="0", Selected=true });
            lst.Add(new SelectListItem { Text="DEACTIVATED", Value="1" });
            return lst;
        }
    }

编辑

以下是我的控制器代码:

public class AdministratorController : Controller
{
    //
    // GET: /Administrator/

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Create()
    {
        return View();
    }

    public ActionResult Read()
    {
        return View();
    }

    public ActionResult Update()
    {
        return View();
    }

    public ActionResult Disable()
    {
        return View();
    }

    public ActionResult Enable()
    {
        return View();
    }
}
4

1 回答 1

2

您必须将 OperatorModel 从您的操作传递给视图:

例如:

  return View(new OperatorModel());
于 2012-08-01T10:20:56.673 回答