我對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 });
}
如何我是否解決這個問題?有什麼想法嗎?
從我所看到的情況來看,您正在爲每個可能導致問題的產品生成表單。爲了隔離這個嘗試,在一個產品中做這個例如'Model.Take(1)'在foreach循環中。 – CrnaStena
@CrnaStena在哪裏? –
'@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