-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 に答える