【问题标题】:"The parameters dictionary contains a null entry for parameter" - How to fix?“参数字典包含参数的空条目” - 如何修复?
【发布时间】:2017-05-18 06:54:09
【问题描述】:

我正在尝试实现一个编辑页面,以便管理员修改数据库中的数据。不幸的是,我遇到了一个错误。

以下代码:

public ViewResult Edit(int productId) {
       // Do something here 
}

但我收到此错误:

"The parameters dictionary contains a null entry for parameter 'productId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult Edit(Int32)' in 'WebUI.Controllers.AdminController'. To make a parameter optional its type should be either a reference type or a Nullable type.
Parameter name: parameters"

我在Global.asax.cs 中更改了我的路线,如下所示:

 routes.MapRoute(
"Admin",
"Admin/{action}/{ productId}",
new { controller = "Admin", action = "Edit",  productId= "" }
);

但我仍然收到错误。

【问题讨论】:

    标签: c# asp.net asp.net-mvc


    【解决方案1】:

    productId 的空字符串(在您的默认路由中)将被框架解析为空条目,并且由于 int 不允许 null...您会收到错误消息。

    变化:

    public ViewResult Edit(int productId)
    

    public ViewResult Edit(int? productId)
    

    如果您想让调用者不必传入产品 ID,这就是您想要根据路由配置方式执行的操作。

    您还可以重新配置默认路由,以便在未提供 productId 时传入一些已知默认值:

    routes.MapRoute( 
        "Admin", 
        "Admin/{action}/{ productId}", 
        new { controller = "Admin", action = "Edit",  productId= -1 } 
    

    【讨论】:

      【解决方案2】:

      我在 Pro ASP.Net 中的 SportStore 示例之后遇到了同样的问题

      解决方案实际上是我的索引视图有以下代码。

      @Html.ActionLink("Edit", "Edit", new { id=item.ProductID }) |
      

      但是我的控制器中的 Edit 方法被定义为

      public ViewResult Edit(int productId)
      

      将我的索引视图更改为阅读

      @Html.ActionLink("Edit", "Edit", new { productId=item.ProductID }) |
      

      解决了问题

      【讨论】:

        【解决方案3】:

        以下是如何忽略任何控制器方法调用的此类参数错误的方法:

        public class MyControllerBase
        {
           //...
        
           protected override void OnActionExecuted(ActionExecutedContext filterContext)
           {
               if (filterContext.Exception != null)
               {
                   var targetSite = filterContext.Exception.TargetSite;
                   if (targetSite.DeclaringType != null)
                       if (targetSite.DeclaringType.FullName == typeof(ActionDescriptor).FullName)
                           if (targetSite.Name == "ExtractParameterFromDictionary")  // Note: may be changed in future MVC versions
                           {
                               filterContext.ExceptionHandled = true;
                               filterContext.Result = new HttpStatusCodeResult((int)HttpStatusCode.BadRequest);
                               return;
                           }
                   //...
                }
                // ...
            }
        }
        

        【讨论】:

          【解决方案4】:

          productId 应限制为int 类型。

          new {controller="Admin", action="Edit"},
          new {productId = @"\d+" }
          

          【讨论】:

            【解决方案5】:

            也许您忘记在视图中传递所需的数据(在本例中为“productId”)。
            我假设您尝试通过单击索引页面中的链接来访问详细信息,我也假设它为“View\Admin\index.cshtml”

                <td>
                    @Html.ActionLink("Edit", "Edit", new { productId = item.ProductId }) |
                    @Html.ActionLink("Details", "Details", new { productId = item.ProductId }) | //note the productId is filled by item.ProductId
                    @Html.ActionLink("Delete", "Delete", new { productId = item.ProductId })
                </td>
            

            不这样做会导致所有参数为空,从而导致错误。

            【讨论】:

              【解决方案6】:

              标准的“int”类 (int32) 不接受 null 值,在这种情况下,它将无法从空字符串转换为 int 并尝试将 null 分配给它。

              我可能会看看您要完成的工作 - 如果您要强制管理员提供 productID 以供他们编辑数据库中的该记录,我会考虑将其放入Request 对象或其他可以提供更多功能的方法。

              【讨论】:

                【解决方案7】:

                在路由上更改它可能会影响使用相同 url 的其他路由。保持简单并涵盖所有基础。

                [HttpGet]
                public ActionResult ThePage(int id = -1)
                {
                    if(id == -1)
                    {
                        return RedirectToAction("Index");
                    }
                
                    //Or proceed as normal
                }
                

                如果这是您在访问页面时遇到的错误,这也很好,因为您需要有一个 id,(例如......人们不应该将 url 放在地址栏中)然后将参数设置为可选值。

                编辑:抱歉,问题不长

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-05-12
                  • 2018-01-21
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-05-12
                  相关资源
                  最近更新 更多