MVC 中页面不存在

发布于 2024-12-07 17:44:08 字数 1649 浏览 0 评论 0原文

我正在按照本教程创建第一个 mvc 应用程序 (创建电影数据库应用程序)

我已经添加了创建视图,但是当我单击“创建新”链接时,该页面不存在。典型的 404 错误

我试过了,

/home/create
/create
/create.aspx
/home/create.aspx

我是 MVC 的新手,所以请不要笑。 :)

编辑: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");

        } 

    }
}

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

        } 

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难忘№最初的完美 2024-12-14 17:44:09

您的控制器中需要一个 Get 和 Post Create 方法。您需要进行以下

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

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

编辑:创建视图的 URL 为 /Home/Create

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

差↓一点笑了 2024-12-14 17:44:09

您没有创建“获取”操作。

基本上,您所执行的创建操作是在提交表单时执行的。

您需要教程中的代码:

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

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();
}  
小帐篷 2024-12-14 17:44:09

您拥有的 Create 用于 HttpPost,当您尝试创建实体时将使用它。最初,您需要一个带有 HttpGet create 方法的控制器方法,它可以让您输入新实体的数据。另外,请确保您的视图位于 Views->home 文件夹中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文