HTML.ActionLink方法 [英] HTML.ActionLink method

查看:333
本文介绍了HTML.ActionLink方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个类

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

在未位于项目文件夹,其中 ItemController 所在,我想创建一个链接登录方法。那么,哪 Html.ActionLink 方法,我应该用我应该通过什么参数?

On a page that is not located at the Item folder, where ItemController resides, I want to create a link to the Login method. So which Html.ActionLink method I should use and what parameters should I pass?

具体而言,我正在寻找替代方法

Specifically, I am looking for the replacement of the method

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

这已经退休在最近的ASP.NET MVC的化身。

that has been retired in the recent ASP.NET MVC incarnation.

推荐答案

我想你想的是这样的:

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的签名:

This uses the following method ActionLink signature:

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的签名:

This uses the following method ActionLink signature:

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).
                )

这避免了硬编码任何路由逻辑进入链接。

This avoids hard-coding any routing logic into the link.

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

这会给你下面的HTML输出,假设:

This will give you the following html output, assuming:

  1. article.Title =标题
  2. article.ArticleID = 5
  3. 您仍然定义了以下路线
  1. article.Title = "Title"
  2. article.ArticleID = 5
  3. you still have the following route defined

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

这篇关于HTML.ActionLink方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆