【问题标题】:Save data instead of adding to database保存数据而不是添加到数据库
【发布时间】:2013-08-21 22:17:22
【问题描述】:

我正在尝试在我的 asp.net mvc 项目中编辑一篇文章。这是我创建项目时所做的:

public ActionResult Create(ArticleViewModel model)
    {
        if (ModelState.IsValid)
        {
            try
            {
                // Get the userID who created the article
                User usr = userrepo.FindByUsername(User.Identity.Name);
                model.UsernameID = usr.user_id;

                repository.AddArticle(model.Title, model.Description, model.ArticleBody);
            }
            catch (ArgumentException ae)
            {
                ModelState.AddModelError("", ae.Message);
            }

            return RedirectToAction("Index");

        }

        return View(model);
    }

在我的存储库中:

public void AddArticle(string Title, string Description, string ArticleBody)
    {
        item Item = new item()
        {
            item_title = Title,
            item_description = Description,
            article_body = ArticleBody,
            item_createddate = DateTime.Now,
            item_approved = false,
            user_id = 1,
            district_id = 2,
            link = "",
            type = GetType("Article")
        };

        try
        {
            AddItem(Item);
        }

        catch (ArgumentException ae)
        {
            throw ae;
        }
        catch (Exception)
        {
            throw new ArgumentException("The authentication provider returned an error. Please verify your entry and try again. " +
                "If the problem persists, please contact your system administrator.");
        }

        Save();
        // Immediately persist the User data

    }

public void AddItem(item item)
    {
        entities.items.Add(item);
    }

但现在我想编辑一篇文章,这就是我现在所拥有的:

public ActionResult Edit(int id)
    {
        var model = repository.GetArticleDetails(id);
        return View(model.ToList());
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(ArticleViewModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the User
            try
            {
                item Item = repository.GetArticleDetailsByTitle(model.Title);

                Item.item_title = model.Title;
                Item.item_description = model.Description;
                Item.article_body = model.ArticleBody.

                // HERE I NEED TO SAVE THE NEW DATA

                return RedirectToAction("Index", "Home");
            }
            catch (ArgumentException ae)
            {
                ModelState.AddModelError("", ae.Message);
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

如您所见,我检查了调整后的文本并将其放入“项目”中。但是我怎样才能把它保存在我的数据库中呢? (我存储库中的函数)

【问题讨论】:

标签: asp.net mysql asp.net-mvc database save


【解决方案1】:

我认为你的save() 方法有entityobject.SaveChanches()

所以你想在这里调用save() 方法

try
            {
                item Item = repository.GetArticleDetailsByTitle(model.Title);

                Item.item_title = model.Title;
                Item.item_description = model.Description;
                Item.article_body = model.ArticleBody.
                **Save();**
                return RedirectToAction("Index", "Home");
            }
  • 应该只需要Save()方法,可以不需要AddItem()方法。

【讨论】:

    【解决方案2】:

    恐怕您需要再次从数据库中获取文章,更新它并保存更改。实体框架会自动跟踪对实体的更改,因此在您的存储库中应该是:

    public void EditArticle(Item article)
    {
        var dbArticle = entities.items.FirstOrDefault(x => x.Id == article.Id);
        dbArticle.item_title = article.item_title;
        //and so on
    
        //this is what you call at the end
        entities.SaveChanges();
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-21
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      • 2016-11-11
      • 2021-07-06
      • 2023-03-13
      相关资源
      最近更新 更多