Search code examples
asp.netasp.net-mvcasp.net-mvc-4model-view-controllerhtml.actionlink

Getting Null value in parameter of Action method


I am getting null value in the parameter of my action method.

this is my action method

 [HttpGet]
    public ActionResult ProjectData(int? formId)
     {
                if (formId == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }

                return View();

            }
     }

and this is my view of actionlink

@foreach (var projects in Model)
                {
                    int formId = Convert.ToInt32(@projects.FormId);

                <tr>
                    <td>@projects.Serial</td>
                    <td>@projects.ProjectName</td>
                    @*<td><a href="#">@projects.SurveyName</a></td>*@
                    @*<td>@Html.ActionLink(@projects.SurveyName, "ActionName", new { id = @projects.FormId })</td>*@

                    <td>@Html.ActionLink(@projects.SurveyName, "ProjectData" , "Home", new {id = formId}, null)</td>
                    <td>@projects.TotalSubmission</td>
                    <td>@projects.LastSubmissionTime</td>
                    <td>@projects.SubmissionToday</td>

                </tr>


                }

I am using list of user defined type model:

@model IEnumerable<MVC.ProjectInformation>

When I click on actionlink under the loop it should send id to the controller actionmethod but I get the parameter null always. Specific Actionlink:

@Html.ActionLink(@projects.SurveyName, "ProjectData" , "Home", new {id = formId}, null)

I can see the id by debugging on the View but on controller actionmethod I cannot get that id value.


Solution

  • The ActionLink's routeValues object properties need to match the parameters of the controller action.

    Currently it is

    new { id = formId }
    

    which would not match the parameter of the ProjectData

    public ActionResult ProjectData(int? formId)
    

    Update to match

    @Html.ActionLink(@projects.SurveyName, "ProjectData" , "Home", new {formId = formId}, null)