0

我有一个“ 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; } 
    }
4

1 回答 1

0

你完全错了。

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

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

请参考

*编辑*

[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");
        }
于 2013-03-30T15:15:00.523 回答