开发者

Page does not exist in MVC

开发者 https://www.devze.com 2023-04-10 20:07 出处:网络
I am following this tutorial to create the first mvc application (Create a Movie Database Application).

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

public ActionResult Create() 
{
    return View();
}

public ActionResult Create([Bind(Exclude = "Id")] Movie movieToCreate) 
{ 
 ....
}

Edit: The URL to your create view is /Home/Create


You dont have a create "get" action.

Basically the create action you have there is for when a form is submitted.

You need this code from the tutorial:

// GET: /Home/Create 
public ActionResult Create()
{
    return View();
}  


The Create you have is for HttpPost which you'll use when you are trying to create an entity. Initially you'll need to have a controller method with HttpGet create method which will let you enter data for a new entity. Also, make sure your view is in Views->home folder.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号