保存数据而不是添加到数据库

时间:2022-04-23 02:34:19

I'm trying to edit an article in my asp.net mvc project. This is what I do when I create a project:

我正在尝试在我的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);
    }

In my repository:

在我的存储库中:

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);
    }

But now I want to edit an article, this is what I have till now:

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

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);
    }

As you can see I check the adjusted text and drop it in "Item". But how can I save this in my database? (the function in my repository)

如您所见,我检查调整后的文本并将其放在“项目”中。但是如何将其保存在我的数据库中? (我的存储库中的函数)

2 个解决方案

#1


1  

I think your save() method had entityobject.SaveChanches()

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

So you want to call that save() method in here

所以你想在这里调用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");
            }
  • should be need to only Save() method, could not need to AddItem() method .
  • 应该只需要Save()方法,不需要AddItem()方法。

#2


0  

I'm afraid you'll need to get article again from database, update it and save changes. Entity framework automatically tracks changes to entites so in your repository should be:

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

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();
}

#1


1  

I think your save() method had entityobject.SaveChanches()

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

So you want to call that save() method in here

所以你想在这里调用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");
            }
  • should be need to only Save() method, could not need to AddItem() method .
  • 应该只需要Save()方法,不需要AddItem()方法。

#2


0  

I'm afraid you'll need to get article again from database, update it and save changes. Entity framework automatically tracks changes to entites so in your repository should be:

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

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();
}