这是错误报告:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Delete(Int32)' in 'Grid.Controllers.GridController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
这是我的代码:
public ActionResult Edit(int ProductId)
{
using (var db = new radioEntities())
{
return View(db.CAT_Products.Find(ProductId));
}
}
我的路由表:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Grid", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
我的操作链接:
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ })
最佳答案
当您的 RouteTable (global.asax.cs) 中有 id
时,您必须为参数使用相同的名称:
//public ActionResult Edit(int ProductId)
public ActionResult Edit(int id)
您应该编辑(修复)ActionLink:
//@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ })
@Html.ActionLink("Edit", "Edit", new { id=item.YourKey }) // depends on your Model and other code
关于c# - 将值传递给 Controller 方法时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11441725/