Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

c# - HTML.ActionLink方法(HTML.ActionLink method)

Let's say I have a class

(假设我有一堂课)

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

On a page that is not located at the Item folder, where ItemController resides, I want to create a link to the Login method.

(在不在ItemController所在的Item文件夹的页面上,我想创建一个指向Login方法的链接。)

So which Html.ActionLink method I should use and what parameters should I pass?

(那么我应该使用哪种Html.ActionLink方法以及我应该传递哪些参数?)

Specifically, I am looking for the replacement of the method

(具体来说,我正在寻找替代方法)

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

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

(已经在最近的ASP.NET MVC化身中退役了。)

  ask by Graviton translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think what you want is this:

(我想你想要的是这个:)

ASP.NET MVC1 (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).
                )

This uses the following method ActionLink signature:

(这使用以下方法ActionLink签名:)

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

ASP.NET MVC2 (ASP.NET MVC2)

two arguments have been switched around

(两个论点已被切换)

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

This uses the following method ActionLink signature:

(这使用以下方法ActionLink签名:)

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

ASP.NET MVC3+ (ASP.NET MVC3 +)

arguments are in the same order as MVC2, however the id value is no longer required:

(参数与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> 

This will give you the following html output, assuming:

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...