code_junkie

HTML.ActionLink method

Nov 11th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. public class ItemController:Controller
  2. {
  3. public ActionResult Login(int id)
  4. {
  5. return View("Hi", id);
  6. }
  7. }
  8.  
  9. Html.ActionLink(article.Title, new { controller = "Articles", action = "Details", id = article.ArticleID })
  10.  
  11. Html.ActionLink(article.Title,
  12. "Login", // <-- Controller Name.
  13. "Item", // <-- ActionMethod
  14. new { id = article.ArticleID }, // <-- Route arguments.
  15. null // <-- htmlArguments .. which are none. You need this value
  16. // otherwise you call the WRONG method ...
  17. // (refer to comments, below).
  18. )
  19.  
  20. public static string ActionLink(this HtmlHelper htmlHelper,
  21. string linkText,
  22. string controllerName,
  23. string actionName,
  24. object values,
  25. object htmlAttributes)
  26.  
  27. Html.ActionLink(article.Title,
  28. "Item", // <-- ActionMethod
  29. "Login", // <-- Controller Name.
  30. new { id = article.ArticleID }, // <-- Route arguments.
  31. null // <-- htmlArguments .. which are none. You need this value
  32. // otherwise you call the WRONG method ...
  33. // (refer to comments, below).
  34. )
  35.  
  36. public static string ActionLink(this HtmlHelper htmlHelper,
  37. string linkText,
  38. string actionName,
  39. string controllerName,
  40. object values,
  41. object htmlAttributes)
  42.  
  43. Html.ActionLink(article.Title,
  44. "Item", // <-- ActionMethod
  45. "Login", // <-- Controller Name.
  46. new { article.ArticleID }, // <-- Route arguments.
  47. null // <-- htmlArguments .. which are none. You need this value
  48. // otherwise you call the WRONG method ...
  49. // (refer to comments, below).
  50. )
  51.  
  52. <a href="/Item/Login/5">Title</a>
  53.  
  54. routes.MapRoute(
  55. "Default", // Route name
  56. "{controller}/{action}/{id}", // URL with parameters
  57. new { controller = "Home", action = "Index", id = "" } // Parameter defaults
  58. );
  59.  
  60. Html.ActionLink(article.Title,
  61. "Login", // <-- ActionMethod
  62. "Item", // <-- Controller Name
  63. new { id = article.ArticleID }, // <-- Route arguments.
  64. null // <-- htmlArguments .. which are none
  65. )
  66.  
  67. <%=Html.ActionLink("Get Involved",
  68. "Show",
  69. "Home",
  70. new
  71. {
  72. id = "GetInvolved"
  73. },
  74. new {
  75. @class = "menuitem",
  76. id = "menu_getinvolved"
  77. }
  78. )%>
  79.  
  80. Html.ActionLink(article.Title, "Login/" + article.ArticleID, 'Item")
Add Comment
Please, Sign In to add comment