0

I am trying to learn MVC development and I am following a tutorial for creating a simple movie application located here - Create a Movie Database Application in 15 Minutes with ASP.NET MVC (C#)

Here are my Edit actions -

public ActionResult Edit(int id)
    {
        var movieToEdit = (from m in _db.Movies
                           where m.Id == id
                           select m).First();
        return View(movieToEdit);
    }

    // POST: Home/Edit/5
    [HttpPost]
    public ActionResult Edit(Movie movieToEdit)
    {
        try
        {
            // TODO: Add update logic here
            var originalMovie = (from m in _db.Movies
                                 where m.Id == movieToEdit.Id
                                 select m).First();

            if (!ModelState.IsValid)
                return View(originalMovie);
            //_db.Entry(movieToEdit).State = System.Data.Entity.EntityState.Modified;
            _db.Entry(movieToEdit).State = EntityState.Modified;
            _db.SaveChanges();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

The guide calls for a line that states _db.ApplyPropertyChanges(originalMovie.EntityKey.EntitySetName, movieToEdit);, however it appears 'ApplyPropertyChanges' is obsolete. I tried using the line that is commented before I added a reference to the System.Data.Entity namespace.

When I run my app, I try to edit a field for one of the rows in the database, but nothing happens when I click the update button. I have a feeling it has something to do with that line that's now deprecated in the tutorial.

1

1 Answer 1

1

agreed you are **looking at a very old version ** (1 and 2) of MVC the current version is 5 or core.., the tutorial is very incomplete, they didn't even show how they assigned the values

This is how you could do it.... which is more inline with the correct version

public ActionResult Edit(Movie model) //renamed from movieToEdit
{
    if (ModelState.IsValid)
    {
        var movie= _db.Movies.Where(m => m.Id == model.Id).SingleOrDefault();            
        movie.x = model.x;
        movie.y = model.y;
        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(model);         
}
Sign up to request clarification or add additional context in comments.

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.