簡體   English   中英

ASP.NET MVC3 - “對象引用未設置為對象的實例”錯誤

[英]ASP.NET MVC3 - “Object reference not set to an instance of an object” error

我對.NET和MVC3比較陌生。 我在嘗試添加對象的實例時遇到上述錯誤消息時遇到了一些問題。 以下是我的代碼:

KeyDate類

public class KeyDate
{
    [Key]
    public int KeyID { get; set; }

    [StringLength(1000), Required]
    public string Title { get; set; }

    [Required]
    public string Description { get; set; }

    [Required]
    [Display(Name = "Event Date")]
    public DateTime EventDate { get; set; }

    [Display(Name = "Event End Date")]
    public DateTime? EventEndDate { get; set; }

    [Display(Name = "Course ID")]
    public int? CourseID { get; set; }

    [Display(Name = "Event ID")]
    public int? EventID { get; set; }

    public DateTime Created { get; set; }

    [Display(Name = "Created By")]
    public string CreatedBy { get; set; }

    public DateTime? Deleted { get; set; }

    [Display(Name = "Deleted By")]
    public string DeletedBy { get; set; }

    public virtual ICollection<Association> Associations { get; set; }
}

協會班

public class Association
{
    [Key, ForeignKey("KeyDate")]
    [Column(Order = 1)]
    public int KeyID { get; set; }

    [Key, ForeignKey("Category")]
    [Column(Order = 2)]
    public int CategoryID { get; set; }

    public virtual KeyDate KeyDate { get; set; }
    public virtual Category Category { get; set; }
}

我在下面發布的控制器中收到錯誤

    [HttpPost]
    public ActionResult Create(KeyDate keyDate, int topic)
    {
        keyDate.Created = DateTime.Now;
        keyDate.CreatedBy = User.Identity.Name;

        // Create topic association
        Association keyDateTopic = new Association();
        keyDateTopic.CategoryID = topic;
        keyDate.Associations.Add(keyDateTopic); // <-- Object reference... error here

        repository.Add(keyDate);
        repository.Save();

        return RedirectToAction("Index");
    }

我已經谷歌搜索了這么多年,並一直遵循ScottGu的“Code First with Existing Database”示例,其中代碼非常相似,但我沒有運氣。 任何幫助贊賞。 謝謝。

Associations為空。 它尚未初始化。

KeyDate添加一個構造KeyDate並在其中初始化它:

public KeyDate()
{
     Associations = new List<Association>();
}

您的Assosiactions List需要在向其添加項目之前進行實例化。

最佳做法是在構造函數中執行此操作。

public class KeyDate
{
...
    public virtual ICollection<Association> Associations { get; set; }
    public KeyDate()
    {
       Associations = new List<Association>()
    }
}

看起來keyDate.Associations為null。

更改

keyDate.Associations.Add(keyDateTopic);

if ( keyDate.Associations == null)
{
   keyDate.Associations = new Collection<Association>();
}

keyDate.Associations.Add(keyDateTopic);

描述

它看起來像keyDate.Associations為null

您有幾種方法可以解決這個問題

樣品

  • 在ActionMethod中創建ICollection

     [HttpPost] public ActionResult Create(KeyDate keyDate, int topic) { keyDate.Created = DateTime.Now; keyDate.CreatedBy = User.Identity.Name; // Create topic association Association keyDateTopic = new Association(); keyDateTopic.CategoryID = topic; // create this list keyDate.Associations = new List<Association>(); keyDate.Associations.Add(keyDateTopic); // <-- Object reference... error here repository.Add(keyDate); repository.Save(); return RedirectToAction("Index"); } 
  • 或者像這樣在KeyDate類中添加構造函數

     public class KeyDate { public KeyDate() { // create this list this.Associations = new List<Association>(); } [Key] public int KeyID { get; set; } [StringLength(1000), Required] public string Title { get; set; } [Required] public string Description { get; set; } [Required] [Display(Name = "Event Date")] public DateTime EventDate { get; set; } [Display(Name = "Event End Date")] public DateTime? EventEndDate { get; set; } [Display(Name = "Course ID")] public int? CourseID { get; set; } [Display(Name = "Event ID")] public int? EventID { get; set; } public DateTime Created { get; set; } [Display(Name = "Created By")] public string CreatedBy { get; set; } public DateTime? Deleted { get; set; } [Display(Name = "Deleted By")] public string DeletedBy { get; set; } public virtual ICollection<Association> Associations { get; set; } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM