Markus Johansson
Markus Johansson

Reputation: 3773

Why is the data not stored when using SubmitChanges?

Using MVC to create a database entry of type Player:

[HttpPost]
public ActionResult Create(FormCollection fc, Player player)
{
    players.Players.InsertOnSubmit(player);
    players.SubmitChanges();

    Errors errors;
    if (!IsValid(player, out errors))
    {
        ViewBag.Errors = errors;
        return RedirectToAction("Edit", player);
    }

    return Redirect("/Home/Players");
}

[HttpPost]
public ActionResult Edit(FormCollection fc, Player player)
{
    players.SubmitChanges();

    return Redirect("/Home/Players");
}

My problem is that players.SubmitChanges() in the Edit method doesn't change anything in the database. Using InsertObSubmit in Create works. Shall I do it in another way?

Upvotes: 0

Views: 75

Answers (2)

Akash Kava
Akash Kava

Reputation: 39956

[HttpPost]
public ActionResult Edit(FormCollection fc, Player player)
{
    // I guess you are forgetting this
    players.Players.AttachAsModified(player)

    players.SubmitChanges();

    return Redirect("/Home/Players");
}

Upvotes: 1

Sergii Kudriavtsev
Sergii Kudriavtsev

Reputation: 10532

When you get a Player value as input parameter for Edit action you should first modify your players collection accordingly - this won't happen automatically - and only then do players.SubmitChanges();

Upvotes: 0

Related Questions