在asp.net mvc控制器方法中,对象引用未设置为对象的实例

时间:2013-03-30 15:08:05

标签: asp.net-mvc nullreferenceexception

我有一个“System.NullReferenceException:对象引用未设置为对象的实例。”每当我尝试插入一篇新文章时出现错误......我在不使用代码的情况下做了同样的事情,即我使用ADO.net数据模型已经存在的数据库[文章表,标签表和ArticleTag表]它的工作原理很好,但这次我试图使用代码首先EF ...我只是希望有人为我调查这个,也许帮助指出我的错误....我可以总是回到数据库,我将使用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; } 
    }

1 个答案:

答案 0 :(得分:0)

你完全错了。

(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");
        }
相关问题