2010-03-23 105 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,只要提問

相關問題