Search code examples
asp.net-mvc-4razorpartial-viewshtml.actionlink

Unable to send parameters in @html.actionlink to partialview


I have one actionlink inside the foreach loop. Depending on the model items I am generating link buttons with values dynamically. This is how it looks

@foreach (var group in Model.records)
{
    <tr>
        <td>@Html.ActionLink(@group.clientId.ToString(), "detailsbyClientId", "DocumentVerification", new { id = @group.clientId.ToString()},null)</td>
        <td>@group.clientName</td>
        <td>@group.Count</td>
    </tr>
}

On clicking link button i am calling partial view as below.

[HttpGet]
public PartialViewResult detailsbyClientId(int? clientId)
{
    return PartialView();
}

I am receiving a null value for clientId but @group.clientId.ToString() contains a valid value.


Solution

  • The parameter in your method is named clientId but your generating a query string value named id. Change one or the other to match

    public PartialViewResult detailsbyClientId(int? ID)
    {
        ....
    }
    

    and in the view

    @Html.ActionLink(@group.clientId.ToString(), "detailsbyClientId", "DocumentVerification", new { id = group.clientId },null)
    

    Note you do not need the @ nor .ToString() in the 4th parameter - just new { id = group.clientId }