【问题标题】:Object reference not set to an instance of an object in asp.net mvc controller method对象引用未设置为 asp.net mvc 控制器方法中的对象实例
【发布时间】:2013-03-21 03:26:07
【问题描述】:

我有一个“ System.NullReferenceException:对象引用未设置为对象的实例。”每当我尝试插入新文章时出错......我在没有首先使用代码的情况下做了同样的事情,即我使用 ADO.net 数据模型来处理已经存在的 DB[Article 表、Tag 表和 ArticleTag 表],它可以工作很好,但这次我尝试先使用代码 EF...我只是希望有人帮我调查一下,也许可以帮助指出我的错误....我总是可以先使用 DB EDMX 但我真的很想弄错。

public class ControlPanelController : Controller
        {
            //
            // GET: /ControlPanel/

           private IPageRepository _repositoryOne;
           private IArticleRepository _repositoryTwo;
            private ITagRepository _repositoryThree;


            public ControlPanelController(IPageRepository repo, IArticleRepository repo2,ITagRepository repo3)
            {
                 _repositoryOne = repo;
                _repositoryTwo = repo2;
                _repositoryThree = repo3;

            }

那是我的控制器.....

下面是给出错误的方法....

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ArticleCreator(string title, string mainBody, string addedBy, DateTime dateAdded, string tags)
        {
            Article article = new Article();
            article.Title = title;
            article.ShortBody = ClassAction.TruncateAtWord(mainBody, 500);
            article.MainBody = mainBody;
            article.DateAdded = dateAdded;
            article.AddedBy = addedBy;
            tags = tags ?? string.Empty;
            string[] tagNames = tags.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string tagName in tagNames)
            {
                article.Tags.Add(GetTag(tagName));

            }

            _repositoryTwo.SaveArticle(article);
            return RedirectToAction("Index");
        }


 private Tag GetTag(string tagName)
        {
            return _repositoryThree.Tags.FirstOrDefault(x => x.Name == tagName) ?? new Tag() { Name = tagName };

        }

我的实体看起来像这样

 public class Article
    {
        [Key]
        [HiddenInput(DisplayValue = false)]
        public int ArticleId { get; set; }

        public string Title { get; set; }
        public string ShortBody { get; set; }
        public string MainBody { get; set; }
        public DateTime DateAdded { get; set; }
        public String AddedBy { get; set; }

        public ICollection<Tag> Tags { get; set; } 


    }


 public class Tag
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }

        public ICollection<Article> Articles { get; set; } 
    }

【问题讨论】:

    标签: asp.net-mvc nullreferenceexception


    【解决方案1】:

    你完全错了。

    (string title, string mainBody, string addedBy, DateTime dateAdded, string tags)
    

    上面的内容是错误的。您不想在操作结果中参数化。您可以传递您的模型类,然后您可以从模型类属性的 ..

    中获取值

    请参考this :

    新建 *编辑*

    [HttpPost]
            [ValidateInput(false)]
            public ActionResult ArticleCreator(string title, string mainBody, string addedBy, DateTime dateAdded, string tags)
            {
                Article article = new Article();
                article.Title = "TestTitle";
                article.ShortBody = "TestBody";
                article.MainBody = "TestMainBody";
                article.DateAdded = DateTime.Now;
                article.AddedBy = "TestName";
                tags = "Test  Tagssss";
                string[] tagNames = tags.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string tagName in tagNames)
                {
                    article.Tags.Add(GetTag(tagName));
    
                }
    
                _repositoryTwo.SaveArticle(article);
                _repositoryTwo.SubmitChanges();
                return RedirectToAction("Index");
            }
    

    【讨论】:

    • 查看我的编辑。我在您的财产中分配了一些值。然后添加 datacontext.submitchanges() 。现在检查一下
    • 问题实际上出在foreach循环中,这一行给出了错误'article.Tags.Add(GetTag(tagName));'
    • 哦。去掉 string.Empty 改一下试试这行 tags = "Test Tagssss";
    猜你喜欢
    • 2020-04-13
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 2013-07-26
    • 2013-10-28
    • 1970-01-01
    • 2013-04-15
    相关资源
    最近更新 更多