0

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)

2
  • What ORM are you using to persist your data to your database? Commented Aug 21, 2013 at 22:24
  • I would refer you to use a repository update method. Take a look at this: [stackoverflow.com/questions/6851147/… Commented Aug 21, 2013 at 23:00

2 Answers 2

1

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 .
Sign up to request clarification or add additional context in comments.

Comments

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:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.