Showing posts with label Resource cannot be found. Show all posts
Showing posts with label Resource cannot be found. Show all posts

Monday, February 28, 2011

MVC3 - Server Error in '/' Application. The resource cannot be found.



Problem:
MVC3 – You added your HttpPost method in your Controller, and you get:

Server Error in '/' Application.


The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /Movies/Edit/1

Code:
        [HttpPost]
        public ActionResult Edit(Movie model)
        {
            try
            {
                var movie = db.Movies.Find(model.ID);

                UpdateModel(movie);
                db.SaveChanges();
                return RedirectToAction("Details", new {id=model.ID});
            }
            catch (Exception)
            {

                ModelState.AddModelError("", "Edit Failure, see inner exception");
            }

            return View(model);
        }



Solution:
You forgot to add the Get method to the Controller class:
Code:
        public ActionResult Edit(int id)
        {
            var movie = db.Movies.Find(id);
            if (movie == null)
                RedirectToAction("Index");
           
            return View(movie);
        }