我正在用MVC5开发一个应用程序,并在上面执行CRUD操作。
我已经成功地将Northwind数据库添加为实体数据模型,并在模型中引入客户。现在,在搭建的帮助下,我生成了CustomersController。
当我在Customer表中创建新记录时。没有问题。
但当我单击该新记录时,Edit、Details和Delete都不起作用。单击以下任一选项后:

将显示以下页面:

我的控制器代码:
namespace MvcNorthwindSample.Controllers
{
public class CustomersController : Controller
{
private NORTHWNDEntities db = new NORTHWNDEntities();
// GET: Customers
public ActionResult Index()
{
return View(db.Customers.ToList());
}
// GET: Customers/Details/5
public ActionResult Details(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Customer customer = db.Customers.Find(id);
if (customer == null)
{
return HttpNotFound();
}
return View(customer);
}
// GET: Customers/Create
public ActionResult Create()
{
return View();
}
// POST: Customers/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax")] Customer customer)
{
if (ModelState.IsValid)
{
db.Customers.Add(customer);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(customer);
}
// GET: Customers/Edit/5
public ActionResult Edit(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Customer customer = db.Customers.Find(id);
if (customer == null)
{
return HttpNotFound();
}
return View(customer);
}
// POST: Customers/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax")] Customer customer)
{
if (ModelState.IsValid)
{
db.Entry(customer).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(customer);
}
// GET: Customers/Delete/5
public ActionResult Delete(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Customer customer = db.Customers.Find(id);
if (customer == null)
{
return HttpNotFound();
}
return View(customer);
}
// POST: Customers/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(string id)
{
Customer customer = db.Customers.Find(id);
db.Customers.Remove(customer);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}我的视图部件:

我在调试时创建代码的结果视图:

发布于 2014-05-21 14:34:23
编辑:发布控制器后:
Customer customer = db.Customers.Find(id);
if (customer == null)
{
return HttpNotFound();
}您确定您的客户的id为1994吗?如果不是,您的逻辑将返回HttpNotFound()。
在你发布的截图中,我可以看到HTTP 404错误信息,请求的网址是/Customer/Edit/1994。
因此,我假设您必须具有以下控制器/操作:
public class CustomerController
{
public ActionResult Edit(int id)
{
return View();
}
}现在大多数人(包括我)犯的最常见的错误是在URL中正确地传递id。您已将id指定为路由模式中的可选参数:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);因此,如果你不想使用id,你可以传递一些其他的东西作为key name in the the query component,例如/Customer/Edit?year=1994。
发布于 2014-05-21 14:32:43
错误是404错误。
它在控制器Customers中查找Edit操作。
在评论中还指出,ID1994后面有一个编码的空格字符。如果假定id为字符串,则可以将参数更改为string类型,而不是int类型
public class CustomersController
{
public ActionResult Edit(int id)
{
return View();
}
}发布于 2014-05-21 16:10:20
我解决了。我终于换了桌子。Northwind数据库的名为Customers的表中存在问题。我下载数据库备份文件。Customer ID列使用插入的值添加一个空格作为默认值。
https://stackoverflow.com/questions/23775539
复制相似问题