我在這兩天試圖解決這個問題。不幸的是,我無法做到這一點。我不明白爲什麼我的「表單」向某些類發送空值。你有沒有想到我必須做什麼?真的這個問題讓我很緊張。MVC3 - 可空類型(表格didn`t發送值控制器)
The parameters dictionary contains a null entry for parameter 'Id_tow' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.RedirectToRouteResult AddToCart(Int32, System.String)' in 'SportsStore.WebUI.Controllers.CartController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: The parameters dictionary contains a null entry for parameter 'Id_tow' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.RedirectToRouteResult AddToCart(Int32, System.String)' in 'SportsStore.WebUI.Controllers.CartController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
下面是這種形式:
@model SportsStore.WebUI.Models.SomeView
@using SportsStore.WebUI.HtmlHelpers
@using SportsStore.WebUI.Controllers
@using SportsStore.Entities
@{
ViewBag.Title = "List";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>List</h2>
@foreach (var p in Model.Towar)
{
<div class="item">
<h3>@p.Nazwa</h3>
@p.Opis
@using(Html.BeginForm("AddToCart", "Cart")) {
@Html.HiddenFor(x => @p.Id_tow)
@Html.Hidden("returnUrl", Request.Url.PathAndQuery)
<input type="submit" value="+ Add to cart" />
}
<h4>@p.Cena.ToString("c")</h4>
</div>
}
<h2>List</h2>
@foreach (var b in Model.Kategorie)
{
<div class="item">
<h3>@b.Nazwa</h3>
</div>
}
<div class="pager">
@Html.PageLinks(Model.PagingInfo, x => Url.Action("List",
new {page = x, category = Model.CurrentCategory}))
</div>
在我看來是顯示正確的價值觀,但發送空類(HTML源代碼)控制器車
<div class="item">
<h3>Piłka</h3>
Piłka Firmy Nike
<form action="/Cart/AddToCart" method="post"><input data-val="true" data-val-number="The field Id_tow must be a number." data-val-required="The Id_tow field is required." id="p_Id_tow" name="p.Id_tow" type="hidden" value="1" /><input id="returnUrl" name="returnUrl" type="hidden" value="/" /><input type="submit" value="+ Add to cart" />
</form>
<h4>59,80 zł</h4>
</div>
其中有一部分得到空值:
public RedirectToRouteResult AddToCart(int Id_tow, string returnUrl)
{
Towar product = repository.Towar
.FirstOrDefault(p => p.Id_tow == Id_tow);
if (product != null)
{
GetCart().AddItem(product, 1);
}
return RedirectToAction("Index", new { returnUrl });
}
如果我會把我的價值觀一切工作正確的,但當然這不是解決問題
public RedirectToRouteResult AddToCart(int Id_tow = 1, string returnUrl = "www.gmail.com")
{
Towar product = repository.Towar
.FirstOrDefault(p => p.Id_tow == Id_tow);
if (product != null)
{
GetCart().AddItem(product, 1);
}
return RedirectToAction("Index", new { returnUrl });
}
此修復程序的問題,如果沒有你的幫助,我將不能夠解決這個問題。由於
@foreach (var p in Model.Towar)
{
<div class="item">
<h3>@p.Nazwa</h3>
@p.Opis
@{
int Id_tow = @p.Id_tow;
}
@using(Html.BeginForm("AddToCart", "Cart")) {
@Html.HiddenFor(x => Id_tow)
@Html.Hidden("returnUrl", Request.Url.PathAndQuery)
<input type="submit" value="+ Add to cart" />
}
<h4>@p.Cena.ToString("c")</h4>
</div>
}
輸入名稱(p.Id_tow)和參數名稱(Id_tow)是不一樣的。 – Hadas 2012-08-08 09:13:19