I am following this tutorial to create the first mvc application (Create a Movie Database Application).
I already added the create view, but when I click on the Create new link, the page does not exist. Typical 404 error.
I tried
/home/create
/create
/create.aspx
/home/create.aspx
I am very newbie at MVC, so please dont laugh. 🙂
EDIT: GLobal .asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
HomeController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Movies.Models;
namespace Movies.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
private LearningEntities _db = new LearningEntities();
public ActionResult Index()
{
return View(_db.Movies1.ToList());
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id")] Movie movieToCreate)
{
if (!ModelState.IsValid)
return View();
_db.AddToMovies1(movieToCreate);
_db.SaveChanges();
return RedirectToAction("Index");
}
}
}
You need a Get as well as Post Create method in your controller. You need the following
Edit: The URL to your create view is
/Home/Create