1

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!

2 Answers 2

2

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" })
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm. Thank you!
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.