[英]ASP.NET MVC3 - “Object reference not set to an instance of an object” error
I'm relatively new to .NET and MVC3. 我对.NET和MVC3比较陌生。 I'm having some trouble with the above error message when trying to add an instance of an object.
我在尝试添加对象的实例时遇到上述错误消息时遇到了一些问题。 Below is my code:
以下是我的代码:
KeyDate class 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; }
}
Association class 协会班
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; }
}
I am getting the error in my Controller posted below 我在下面发布的控制器中收到错误
[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");
}
I have Googled this for ages, and have been following ScottGu's "Code First with Existing Database" example in which the code is quite similar, but I have had no luck. 我已经谷歌搜索了这么多年,并一直遵循ScottGu的“Code First with Existing Database”示例,其中代码非常相似,但我没有运气。 Any help appreciated.
任何帮助赞赏。 Thanks.
谢谢。
Associations
is null. Associations
为空。 It has not been initialised. 它尚未初始化。
Add a constructor for KeyDate
and init it in there: 为
KeyDate
添加一个构造KeyDate
并在其中初始化它:
public KeyDate()
{
Associations = new List<Association>();
}
Your Assosiactions List needs to be instantiated before adding items to it. 您的Assosiactions List需要在向其添加项目之前进行实例化。
Best practice is to do this from within the constructor. 最佳做法是在构造函数中执行此操作。
public class KeyDate
{
...
public virtual ICollection<Association> Associations { get; set; }
public KeyDate()
{
Associations = new List<Association>()
}
}
Looks like keyDate.Associations
is null. 看起来
keyDate.Associations
为null。
Change 更改
keyDate.Associations.Add(keyDateTopic);
to 至
if ( keyDate.Associations == null)
{
keyDate.Associations = new Collection<Association>();
}
keyDate.Associations.Add(keyDateTopic);
It looks like keyDate.Associations
is null 它看起来像
keyDate.Associations
为null
You have several ways to solve this 您有几种方法可以解决这个问题
Create the ICollection
in your ActionMethod. 在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"); }
OR Add a constructor to your KeyDate
class like this 或者像这样在
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.