MVC 中页面不存在
我正在按照本教程创建第一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的控制器中需要一个 Get 和 Post Create 方法。您需要进行以下
编辑:创建视图的 URL 为
/Home/Create
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
您没有创建“获取”操作。
基本上,您所执行的创建操作是在提交表单时执行的。
您需要教程中的代码:
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:
您拥有的 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.