c# - 将值传递给 Controller ​​方法时出错

标签 c# asp.net asp.net-mvc-3 entity-framework

这是错误报告:

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/

相关文章:

c# - Apsx.net C# 防止单击按钮时重新加载页面

c# - 带有 jQ​​uery Ajax 调用的 MVC 不能正确绑定(bind)空数组/可枚举

asp.net - NodaTime 转换(第 2 部分)。如何?

c# - 内存中非常大的字符串

c# - 如何从 Azure Blob 存储获取当前修改的文件

asp.net - 如何在 .asp 页面打开时调用 global.asax

c# - CustomValidator 未在控制中触发

c# - 无法删除从 C# 应用程序启动的子进程中的文件

c# - Kendo 网格导出到 Excel 货币格式

.net - ASP.Net MVC2到MVC3升级-Assembly引用问题