如何使用 actionlink 将参数传递给控制器

How to pass parameter to controller using actionlink

我需要将视图数据(整数)传递给另一个控制器。

这是我试过的;

@Html.ActionLink("Get Location", "Index", "Map", new { Id=@item.Id},null)

我需要将此信息传递给 "Map" 控制器的索引操作方法;

  public ActionResult Index(int? i)
    {
        var Id = from o in db.Objects where o.Id == i select o;
        return View(Id);
    }

但是参数没有通过...这是传递参数的方式吗??当我设置断点时我发现了那个int?我是空的..这是为什么?

您传递的参数是 Id,但您的操作中的参数是 i。

将 i 重命名为 Id。

Html.ActionLink("Get Location", "Index", "Map", new { id=@item.Id},null)


public ActionResult Index(int id)
{
    var Id = from o in db.Objects where o.Id == id select o;
    return View(Id);
}