我正在按照本教程创建第一个 mvc
应用程序 ( Create a Movie Database Application) 。
我已经添加了创建 View ,但是当我单击“创建新”链接时,该页面不存在。典型的 404 错误
。
我试过了
/home/create
/create
/create.aspx
/home/create.aspx
我是 MVC
的新手,所以请不要笑。 :)
编辑:全局.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
);
}
家庭 Controller
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");
}
}
}
最佳答案
您的 Controller 中需要一个 Get 和 Post Create 方法。您需要以下内容
public ActionResult Create()
{
return View();
}
public ActionResult Create([Bind(Exclude = "Id")] Movie movieToCreate)
{
....
}
编辑:创建 View 的 URL 为 /Home/Create
关于asp.net - MVC 中页面不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7649308/