【问题标题】:Error when passing value to controller method将值传递给控制器​​方法时出错
【发布时间】:2012-07-11 21:24:30
【问题描述】:

这是错误报告:

参数字典包含参数“id”的空条目 方法的不可空类型“System.Int32” 'System.Web.Mvc.ActionResult 删除(Int32)' 在 'Grid.Controllers.GridController'。可选参数必须是 引用类型,可为空的类型,或被声明为可选 范围。参数名称:参数

这是我的代码:

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 */ })

【问题讨论】:

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


    【解决方案1】:

    当您的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 2012-11-24
      • 1970-01-01
      相关资源
      最近更新 更多