nielsv
nielsv

Reputation: 6800

Save data instead of adding to database

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

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)

Upvotes: 0

Views: 125

Answers (2)

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38663

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

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

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 .

Upvotes: 1

VsMaX
VsMaX

Reputation: 1785

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:

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

Upvotes: 0

Related Questions