在控制器中单击的MVC请求Actionlink

时间:2015-07-26 14:11:11

标签: html asp.net-mvc actionlink

我有做类似功能的动作链接。如何请求在控制器中单击哪个actionlink,以便我可以将它们全部放在相同的TopRated函数中,如下所示:

@Html.ActionLink("Today", "TopRated")
@Html.ActionLink("30 Days", "TopRated")
@Html.ActionLink("Weekly", "TopRated")
@Html.ActionLink("All Time", "TopRated")

而不是将它们放在4个不同的函数中:

@Html.ActionLink("Today", "Today")
@Html.ActionLink("30 Days", "TopRated30")
@Html.ActionLink("Weekly", "TopRatedWeekly")
@Html.ActionLink("All Time", "AllTimeTopRated")

控制器:

 public ActionResult TopRated()
        {
            using (var context = new EntityDBContext())
            {
                //Request which actionlink was just clicked by user
            }
        }

1 个答案:

答案 0 :(得分:3)

创建所有评分最高的时段的枚举器:

public enum PeriodEnum
{
   Today,
   ThirtyDays,
   Weekly,
   AllTime
}

您的TopRated应该会收到您之前创建的枚举类型的参数:

public ActionResult TopRated(PeriodEnum period)
{
    using (var context = new EntityDBContext())
    {
         //Request which actionlink was just clicked by user
    }
}

最后,您的操作链接将如下所示:

@Html.ActionLink("Today", "TopRated", new {period = PeriodEnum.Today})
@Html.ActionLink("30 Days", "TopRated", new {period = PeriodEnum.ThirtyDays})})
@Html.ActionLink("Weekly", "TopRated", new {period = PeriodEnum.Weekly})
@Html.ActionLink("All Time", "TopRated", new {period = PeriodEnum.AllTime})