将ViewBag值从ActionLink传递到Controller

时间:2015-05-28 15:43:38

标签: asp.net-mvc null actionlink

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

布局Cshtml页面:

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

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

Controller ActionResult:

 public ActionResult ViewComments(long? privApplicationId)

你能帮帮我吗?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

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