Asp.net mvc 3 MVC3自动生成代码错误
嗨,我正在做一个MVC3应用程序。我使用的是实体框架数据库方法第一种方法。所以所有的代码都是自动生成的。但问题是当我点击“编辑”、“详细信息”和“删除”链接时,我得到了这个错误Asp.net mvc 3 MVC3自动生成代码错误,asp.net-mvc-3,Asp.net Mvc 3,嗨,我正在做一个MVC3应用程序。我使用的是实体框架数据库方法第一种方法。所以所有的代码都是自动生成的。但问题是当我点击“编辑”、“详细信息”和“删除”链接时,我得到了这个错误 The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)' in 'F
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)' in 'Fin_trial_06.Controllers.AuthorController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters.
我的控制器代码是:
public ActionResult Details(int? id)
{
Author authors = _entities.Authors.Single(n => n.Author_ID == id);
return View(authors);
}
public ActionResult Edit(int id)
{
return View(_entities.Authors.Find(id));
}
// POST: /Author/Edit/
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, Author aut)
{
if (id == null)
return View("AuthorNotFound");
try
{
_entities.Entry(aut).State = EntityState.Modified;
_entities.SaveChanges();
TempData["Message"] = "You have successfully Editied Author";
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult Delete(int id)
{
return View(_entities.Authors.Find(id));
}
// POST: /Author/Delete/5
[HttpPost]
public ActionResult Delete(int id, Author aut)
{
try
{
_entities.Entry(aut).State = EntityState.Deleted;
_entities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
请帮忙?嗯,这个信息似乎很清楚
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, Author aut)
{
if (id == null)
如果id==null,则无法输入操作
小后果:
if (id == null)
没用
解决方案:
不管你说id是一个
int ?
或者,不要忘记在编辑视图中为Id添加一个隐藏字段在该方法中添加一个断点,以查看Id是否正在传递给编辑函数。如果不是,那么您的视图是错误的,并且没有传递要编辑的项目的id。如果你发布你的视图代码,我可以告诉你它失败的地方。@Raphael将参数设为空并不能解决他的问题。如果他将空id传递给编辑函数,该函数如何知道要编辑哪个项?