Reputation: 13
Here is my view:
@foreach (var item in Model)
{
if(i == 0)
{
@:<div class="row">
}
<div class="col-md-3 col-xs-12" style="margin-top:20px;">
@Html.ActionLink(item.Name, "ClientDetail", "Client", null, new { @class = "btn btn-primary btn-block", id = @item.id})
</div>
i++;
if(i%4 == 0)
{
@:</div>
i = 0;
}
}
and here is the controller:
public ActionResult ClientDetail(int id)
{
ClientModel model = cserv.Get(id, User.Identity.GetUserId());
ClientViewModel viewModel = new ClientViewModel(model);
return View(viewModel);
}
Every time I click on one of my links it gives me this error:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult ClientDetail(Int32)' in 'TechUCRM.Controllers.ClientController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
Why is my id parameter not being passed into the controller?
Thank you!
Upvotes: 1
Views: 1603
Reputation: 599
change this line:
@Html.ActionLink(item.Name, "ClientDetail", "Client", null, new { @class = "btn btn-primary btn-block", id = @item.id})
to:
@Html.ActionLink(item.Name, "ClientDetail", "Client", new{ id= item.Id }, new { @class = "btn btn-primary btn-block"})
one you used up there sets id attribute of HTML. it is not the Id for Routing.
Upvotes: 1
Reputation: 62301
You want to place id at routeValues argument.
@Html.ActionLink(item.Name, "ClientDetail", "Client",
new { id = item.id},
new { @class = "btn btn-primary btn-block"})
Same as
@Html.ActionLink(item.Name, "ClientDetail", "Client",
routeValues: new { id = item.id },
htmlAttributes: new { @class = "btn btn-primary btn-block" })
Upvotes: 2