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


2 comments:

  1. public class StoreController : Controller
    {
    MusicStoreEntities StoreDB = new MusicStoreEntities();


    public ActionResult Index()
    {
    //var genres = new List
    var genres = StoreDB.genres.ToList();
    // {
    // new Genre {Name="Disco"},
    // new Genre{Name="Razz"},
    // new Genre{Name="Rock"}

    //};
    return View (genres );

    ReplyDelete