2016-03-02 32 views
2

我對asp.net mvc完全陌生,所以請在解釋我的問題時與我同時露面。 我有如下一種觀點:我該如何解決參數字典包含非空的類型'System.Int32'參數'productId'的空條目?

<div class="col-sm-9 padding-right"> 
       <div id="bookListDiv"> 
        <!--features_items --> 
        <h2 class="title text-center">Features Items</h2> 
        @{Html.RenderAction("ProductList", "Product", new { Model = Model });} 
       </div> 
      </div> 

的視圖中,你可以看到,我已經渲染的作用,這是下面:

[ChildActionOnly] 
     public ActionResult ProductList(List<Product> Model) 
     { 
      return PartialView(Model); 
     } 

上述呼籲的行動部分ProductList局部視圖,這在下面在這裏:

@model List<EShopperTheme.Domain.Entities.Product> 

@foreach (var item in Model) 
{ 
    <div class="col-sm-4"> 
     <div class="product-image-wrapper"> 
      <div class="single-products"> 
       <div class="productinfo text-center"> 
        <img src="@Url.Action("GetMainPicture", "Product", new { item.ProductID })" alt="" /> 
        <h2>@item.ProductCategory</h2> 
        <h2>@item.ProductPrice AFN</h2> 
        <p>@item.ProductName</p> 

        <a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a> 
       </div> 
       <div class="product-overlay"> 
        <div class="overlay-content"> 
         <img src="@Url.Action("GetSecondPicture", "Product", new { item.ProductID })" alt="" /> 
         <h2>@item.ProductPrice AFN</h2> 
         <p>@Html.ActionLink(item.ProductName, "ProductDetails", new { item.ProductID })</p> 
         @using (Html.BeginForm("AddToCart", "Cart")) 
         { 
          @Html.HiddenFor(x => item.ProductID) 
          @Html.Hidden("returnUrl", Request.Url.PathAndQuery) 
          <input type="submit" class="btn btn-default add-to-cart" value="Add to cart" /> 
         } 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
} 

正如你可以在部分我有一個表格和表格內有提交的按鈕類型看210。問題是當我點擊添加到購物車按鈕,而運行應用程序時,我得到這個錯誤。 異常詳細信息:System.ArgumentException:參數字典包含參數無效項非可空類型

'System.Int32' for method 'System.Web.Mvc.RedirectToRouteResult AddToCart(EShopperTheme.Domain.Entities.Cart, Int32, System.String)' in 'EShopperTheme.Controllers.CartController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. 
Parameter name: parameters 

在我的車控制器的操作AddToCart的「產品ID」低於:

public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl) 
     { 
      Product product = repository.Products 
         .FirstOrDefault(p => p.ProductID == productId); 
      if (product != null) 
      { 
       cart.AddItem(product, 1); 
      } 
      return RedirectToAction("Index", new { returnUrl }); 
     } 

如何我是否解決這個問題?有什麼想法嗎?

+0

從我所看到的情況來看,您正在爲每個可能導致問題的產品生成表單。爲了隔離這個嘗試,在一個產品中做這個例如'Model.Take(1)'在foreach循環中。 – CrnaStena

+0

@CrnaStena在哪裏? –

+0

'@foreach(Model.Take(1)中的var item)'。但讓我問你這個。這是否應該將項目添加到購物車,然後重新加載相同的頁面?如果是這樣,我會指出你爲了這個目的而使用Ajax帖子的方向。看到這個[文章](http://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-8)對於較老版本的MVC,原理會類似到你想要達到的目標。 – CrnaStena

回答

1

您使用的@Html.HiddenFor(x => item.ProductID)生成

<input type="hidden" name="item.ProductID" .. /> 

一個隱藏的輸入,但將需要`名稱=「產品」。刪除輸入,而使用路由值形式

@using (Html.BeginForm("AddToCart", "Cart", new { ProductID = item.ProductID})) 

另外,您可以使用

@Html.Hidden("ProductID", item.ProductID) 

旁註:你的孩子的行動是不必要的,你可以只使用@{ Html.RenderPartial("ProductList", Model;}視圖

+0

這兩個建議的工作原理:P THanks –

+1

第一個更好(你也應該添加返回url作爲路由參數。隱藏的輸入是不必要的,它的創建無效的HTML,除非你也使用'new {id =「」}'去除重複的'id'屬性 –

+0

Hey Stephen,你什麼時候睡覺?所以我可以在你做這件事的時候回答幾個問題。 :) – CrnaStena

1

嘗試改變:

@Html.HiddenFor(x => item.ProductID) 

@Html.Hidden("productId", item.ProductID) 
+0

感謝您的回答,這是一個很好的建議,但我已經收到了答案。 :) –

相關問題