在 Razor 视图中调用另一个控制器的方法并传递参数

in Razor view calling method of another controller and passing parameters

在 ClimateChartController 的视图中,我执行以下代码:

@Html.ActionLink("Kies land", "ListCountries", "Continent" new {Selectedyear = @ViewBag.SchoolYear, continentId = @ViewBag.ContinentId})

所以这应该转到 ContinentController 的方法 ListCountries 以及给定的参数。

现在这行不通了,如果我在没有参数的情况下执行它,它会转到方法但是好吧,我需要参数...

现在我通过在 ClimateChartController 中使用以下方法解决了这个问题:

public ActionResult ListCountries(int selectedyear, int continentid)
        {
            return RedirectToAction("ListCountries", "Continent",
                new { selectedYear = selectedyear, continentId = continentid });
        }

这按预期工作,但会导致代码混乱且不整洁。

那么如何调用另一个控制器的方法并传递一些参数呢?

试试这个:

@Html.ActionLink("Kies land", "ListCountries", "Continent" , null, new {Selectedyear = @ViewBag.SchoolYear, continentId = @ViewBag.ContinentId})

或:

Html.ActionLink("Kies land", "ListCountries", "Continent", new {Selectedyear = @ViewBag.SchoolYear, continentId = @ViewBag.ContinentId}, null)

这里有可能的解决方案:

Why does Html.ActionLink render "?Length=4"