简体   繁体   English

ASP.NET MVC3 - “对象引用未设置为对象的实例”错误

[英]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);

Description 描述

It looks like keyDate.Associations is null 它看起来像keyDate.Associations为null

You have several ways to solve this 您有几种方法可以解决这个问题

Sample 样品

  • 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.

相关问题 Asp.Net MVC4&#39;对象引用未设置为对象的实例&#39; - Asp.Net MVC4 'Object reference not set to an instance of an object' ASP.NET MVC对象引用未设置为对象的实例 - ASP.NET MVC Object reference not set to an instance of an object ASP.NET MVC“'对象引用未设置为 object 的实例。'”错误 - ASP.NET MVC "'Object reference not set to an instance of an object.'" Error Object 参考未设置为 object 的实例? (异常错误~ASP.NET MVC) - Object Reference not set to an instance of an object? (Exception error ~ ASP.NET MVC) 尝试使用 ASP.NET MVC 上传 PDF 文件导致错误“对象引用未设置为对象的实例” - Trying to upload a PDF file using ASP.NET MVC is causing an error "Object reference not set to an instance of an object" ASP.NET MVC | DropDownListFor给出错误的对象引用未设置为对象的实例 - ASP.NET MVC | DropDownListFor gives error Object reference not set to an instance of an object NullReferenceException:Object 引用未设置为 ASP.NET MVC 中的 object 错误的实例 - NullReferenceException: Object reference not set to an instance of an object error in ASP.NET MVC ASP.NET MVC对象参考错误 - ASP.NET MVC Object reference error 使用ASP.NET 5和MVC 6检查会话是否为空时,获取NullReferenceException和对象引用未设置为对象的实例 - Getting NullReferenceException and object reference not set to an instance of an object when checking if session is null with ASP.NET 5 and MVC 6 Facebook登录Asp.net Mvc对象引用未设置为对象的实例 - Facebook Login Asp.net Mvc Object reference not set to an instance of an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM