将值传递给控制器​​方法时出错

Far*_*tán 1 c# asp.net entity-framework asp.net-mvc-3

这是错误报告:

参数字典包含非可空类型'System.Int32'的参数'id'的空条目,用于'Grid.Controllers.GridController'中的方法'System.Web.Mvc.ActionResult Delete(Int32)'.可选参数必须是引用类型,可空类型,或者声明为可选参数.参数名称:参数

这是我的代码:

public ActionResult Edit(int ProductId)
    {
        using (var db = new radioEntities())
        {
            return View(db.CAT_Products.Find(ProductId));
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的路线表:

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
        );

    }
Run Code Online (Sandbox Code Playgroud)

我的行动链接:

@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ })
Run Code Online (Sandbox Code Playgroud)

Hen*_*man 5

如果id你的RouteTable(global.asax.cs)中有一个,那么你必须为param使用相同的名称:

//public ActionResult Edit(int ProductId)
public ActionResult Edit(int id)
Run Code Online (Sandbox Code Playgroud)

您应该编辑(修复)ActionLink:

//@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ })
@Html.ActionLink("Edit", "Edit", new { id=item.YourKey })  // depends on your Model and other code
Run Code Online (Sandbox Code Playgroud)