将 ViewBag 值从 ActionLink 传递到控制器

Passing ViewBag value from ActionLink to Controller

我正在尝试将 privApplicationId(从 ViewBag 检索)传递到控制器中的 ActionResult

布局 Cshtml 页面:

 @Html.ActionLink("Comments", "ViewComments", "SubmittedApplications", new { @area = "Privileging" }, new { privApplicationId = @ViewBag.PrivApplicationId })

但是,每次进入 ActionResult 时,privApplicationId 值为 null。

控制器操作结果:

 public ActionResult ViewComments(long? privApplicationId)

你能帮帮我吗?

您选择了错误的 ActionLink 助手重载。 new { privApplicationId = @ViewBag.PrivApplicationId } 作为 htmlAttribute 参数传递,但应该是 routeValue。试试这个方法:

@Html.ActionLink("Comments", "ViewComments", "SubmittedApplications",
      new { area = "Privileging", privApplicationId = @ViewBag.PrivApplicationId }, null)

ViewBag 中删除 @ 并将 null 作为最后一个参数传递。 这应该有效

@Html.ActionLink("Comments", 
                 "ViewComments", 
                 "SubmittedApplications", 
                  new 
                  { 
                      area = "Privileging", 
                      privApplicationId = ViewBag.PrivApplicationId 
                  }, 
                  null)