c# - HTML.ActionLink 方法

标签 c# .net asp.net-mvc html-helper actionlink

假设我有一个类

public class ItemController:Controller
{
    public ActionResult Login(int id)
    {
        return View("Hi", id);
    }
}

在不位于 ItemController 所在的 Item 文件夹中的页面上,我想创建一个指向 Login 方法的链接。那么我应该使用哪种 Html.ActionLink 方法以及我应该传递哪些参数?

具体来说,我正在寻找替换的方法

Html.ActionLink(article.Title,
    new { controller = "Articles", action = "Details",
          id = article.ArticleID })

在最近的 ASP.NET MVC 化身中已经停用。

最佳答案

我想你想要的是这个:

ASP.NET MVC1

Html.ActionLink(article.Title, 
                "Login",  // <-- Controller Name.
                "Item",   // <-- ActionMethod
                new { id = article.ArticleID }, // <-- Route arguments.
                null  // <-- htmlArguments .. which are none. You need this value
                      //     otherwise you call the WRONG method ...
                      //     (refer to comments, below).
                )

这使用以下方法 ActionLink 签名:

public static string ActionLink(this HtmlHelper htmlHelper, 
                                string linkText,
                                string controllerName,
                                string actionName,
                                object values, 
                                object htmlAttributes)

ASP.NET MVC2

调换了两个参数

Html.ActionLink(article.Title, 
                "Item",   // <-- ActionMethod
                "Login",  // <-- Controller Name.
                new { id = article.ArticleID }, // <-- Route arguments.
                null  // <-- htmlArguments .. which are none. You need this value
                      //     otherwise you call the WRONG method ...
                      //     (refer to comments, below).
                )

这使用以下方法 ActionLink 签名:

public static string ActionLink(this HtmlHelper htmlHelper, 
                                string linkText,
                                string actionName,
                                string controllerName,
                                object values, 
                                object htmlAttributes)

ASP.NET MVC3+

参数的顺序与 MVC2 相同,但不再需要 id 值:

Html.ActionLink(article.Title, 
                "Item",   // <-- ActionMethod
                "Login",  // <-- Controller Name.
                new { article.ArticleID }, // <-- Route arguments.
                null  // <-- htmlArguments .. which are none. You need this value
                      //     otherwise you call the WRONG method ...
                      //     (refer to comments, below).
                )

这避免了将任何路由逻辑硬编码到链接中。

 <a href="/Item/Login/5">Title</a> 

这将为您提供以下 html 输出,假设:

  1. article.Title = "标题"
  2. article.ArticleID = 5
  3. 您仍然定义了以下路线

. .

routes.MapRoute(
    "Default",     // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

关于c# - HTML.ActionLink 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/200476/

相关文章:

c# - 绑定(bind)到通用属性

java - .NET 中 Java Gzip 解压错误

c# - 在 Razor 应用程序中重定向到另一个网站

c# - 续 MVC 区域 404 错误。无计可施

c# - 如何在两个位置之间连续移动 GameObject?

c# - 诊断 'CodeContracts requires unproven: constructor != null'?

c# - Owin startup.cs 中的 ModelBinders 注册

.net - .NET 音译库是否存在?

c# - Hangfire 具有水平缩放功能

c# - 了解 VS2010 C# 并行分析结果