![](/img/trans.png)
[英]Asp.Net MVC4 'Object reference not set to an instance of an object'
[英]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.