Reputation: 494
I trying resolve this problem during this two days. Unfortunately I am not able to do it. I don`t understand why my "form" send null values to some class. Have you hot idea what I have to do? Really this problem is nervous me.
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.
Here is this form:
@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>
In my opinion is show correct values but send null to class (HTML source)
<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>
Part of Controller Cart which get nulls values:
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 });
}
If I will put my values everything working correct but of course this not fix a problem
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 });
}
This fix problem if not your help I will be not able to fix this. Thanks
@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>
}
Upvotes: 0
Views: 1225
Reputation: 10766
You could replace
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 });
}
with
public RedirectToRouteResult AddToCart(Towar tow, string returnUrl)
{
Towar product = repository.Towar
.FirstOrDefault(p => p.Id_tow == tow.Id_tow);
if (product != null)
{
GetCart().AddItem(product, 1);
}
return RedirectToAction("Index", new { returnUrl });
}
Or you could use Html.Hidden("Id_tow", @p.Id_tow)
but this would not be recommended as it creates a bit of a brittle dependency between the View and the Controller.
Upvotes: 1
Reputation: 3199
The problem can be seen in the following section of the generated hmtl:
id="p_Id_tow" name="p.Id_tow"
this difference in names prevents the mvc routing engine from associating the values of the hidden field with the parameter named Id_tow. The simplest solution is to change the name of the paramter to match the id or name of the field.
Upvotes: 1