Html.Action没有将参数传递给Action Method

时间:2016-12-17 05:55:20

标签: asp.net-mvc

我的动作方法签名是:

public ActionResult ViewContest(int id, int showNext){
//Some code here
}

在我的一个观点中,我有这个:

<a href="@Html.Action("ViewContest", "Home", new { id = Model.UserActivity.UserParticipation.CompetitionId, showNext = 1 })">
    <img src="~/Image/RightArrow.png" style="float:right;width:40px;" />
</a>

当我运行程序时,视图会正确地将“id”值传递给方法,但运行时会抛出showNext为null的异常。为什么我的showNext值没有传递给方法?有什么想法吗?

1 个答案:

答案 0 :(得分:1)

你使用了错误的助手。要生成ViewContest控制器使用的Home方法的网址:

@Url.Action("ViewContest", "Home", new { id = Model.UserActivity.UserParticipation.CompetitionId, showNext = 1 })

所以最终你的代码应该是这样的:

<a href="@Url.Action("ViewContest", "Home", new { id = Model.UserActivity.UserParticipation.CompetitionId, showNext = 1 })">
    <img src="~/Image/RightArrow.png" style="float:right;width:40px;" />
</a>
相关问题