HTML.ActionLink method

前端 未结 10 1402
刺人心
刺人心 2020-11-22 11:07

Let\'s say I have a class

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


        
10条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 11:35

    I think what you want is this:

    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:

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

    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:

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

    ASP.NET MVC3+

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

    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.

     Title 
    

    This will give you the following html output, assuming:

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

提交回复
热议问题