代码之家  ›  专栏  ›  技术社区  ›  Rod

我可以通过Helper类构建ActionLink吗?怎么用?

  •  0
  • Rod  · 技术社区  · 16 年前

    <a href='/controller/action?id=val'></a>
    

    这与 Html.ActionLink() ,id被接受为控制器内部方法中的参数。如果我只是生成 a 当我尝试用id参数定义方法时,我得到了下面的错误:

    The parameters dictionary contains a null entry for parameter 'id' 
    of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult' 
    ...
    
    To make a parameter optional its type should be either a reference    
    type or a Nullable type.
    Parameter name: parameters
    1. 有办法使用吗 在我的助手班?
    2. 为什么这两种技术之间存在差异?

    public ActionResult AssignmentAdd(int id)
    {
       return View();
    }
    
    1 回复  |  直到 16 年前
        1
  •  2
  •   Darin Dimitrov    16 年前

    public ActionResult Action(int? id) 
    { }
    

    此外,如果您遵循模板生成的默认路由表,则此操作的正确链接应该是:

    <a href="/controller/action/val"></a>
    

    <%= Html.ActionLink("link text", "action", new { id = "val" }) %>