2010-03-23 101 views
37

我正在尝试执行编辑页面,以便管理员修改数据库中的数据。不幸的是,我遇到了一个错误。“参数字典包含参数的空条目” - 如何解决?

下面的代码:

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= "" } 
); 

但还是我得到错误 。

回答

44

productId(在您的默认路由中)的空字符串将被Framework解析为空条目,并且由于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 } 
0

标准“INT”类(INT32)不接受空值,而且它在这种情况下,会将空字符串强制转换为int,并尝试将null赋值给它。

我可能会看看你想要完成的任务 - 如果你试图强制管理员为他们提供一个productID来编辑数据库中的记录,我会考虑把这个到Request对象或其他一些可以提供更多功能的方法中。

4

productId应限制为int类型。

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

也许你忘了在视图中传递所需的数据(在本例中为'productId')。
我假设你尝试通过单击索引页,我还以为它是“视图\管理\ 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> 

失败的链接来访问细节这样做会导致所有PARAMS是null因此导致错误。

6

这里是如何忽略任何控制器的方法调用这种说法错误的方式:

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; 
        } 
      //... 
     } 
     // ... 
    } 
} 
15

我碰到了同样的问题来了之后在Pro ASP.Net

的解决方案是在工作SportStore例子实际上我的索引视图具有以下代码。

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

然而,在我的控制我的编辑方法定义为

public ViewResult Edit(int productId) 

改变我的索引视图阅读

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

固定的问题

0

路线上更改它会使牵涉其他使用相同网址的路线。保持简单并覆盖所有基地。

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

    //Or proceed as normal 
} 

这也是伟大的,如果这是你得到一个错误,当你,因为你需要有一个id访问该页面,(例如......人们把URL中时,他们的地址栏不应该)然后将参数设置为可选值。

编辑:抱歉int,只要提问

相关问题