HTML.Metoda ActionLink
Powiedzmy, że mam klasę
public class ItemController:Controller
{
public ActionResult Login(int id)
{
return View("Hi", id);
}
}
Na stronie, która nie znajduje się w folderze Item, gdzie znajduje się ItemController
, chcę utworzyć link do metody Login
. Jaką metodę Html.ActionLink
powinienem zastosować i jakie parametry przekazać?
W szczególności Szukam zamiennika metody
Html.ActionLink(article.Title,
new { controller = "Articles", action = "Details",
id = article.ArticleID })
Który został wycofany w ostatnim ASP.NET wcielenie MVC.
10 answers
Myślę, że to czego chcesz to:
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).
)
Używa się następującej metody sygnatury ActionLink:
public static string ActionLink(this HtmlHelper htmlHelper,
string linkText,
string controllerName,
string actionName,
object values,
object htmlAttributes)
ASP.NET MVC2
dwa argumenty zostały zamienione
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).
)
Używa się następującej metody sygnatury ActionLink:
public static string ActionLink(this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
object values,
object htmlAttributes)
ASP.NET MVC3 +
argumenty są w tej samej kolejności co MVC2, jednak wartość id nie jest już wymagana:
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).
)
Pozwala to uniknąć twardego kodowania jakichkolwiek przekierowanie logiki do łącza.
<a href="/Item/Login/5">Title</a>
To da Ci następujące wyjście html, zakładając, że:
article.Title = "Title"
article.ArticleID = 5
- nadal masz zdefiniowaną następującą trasę
. .
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-22 05:55:54
Chciałem dodać do odpowiedź Josepha Kingry'ego . Podal rozwiazanie, ale na poczatku tez nie moglem go uruchomic i uzyskalem wynik tak jak Adhip Gupta. I wtedy zdałem sobie sprawę, że trasa musi istnieć w pierwszej kolejności, a parametry muszą dokładnie pasować do trasy. Miałem więc id, a następnie parametr tekstowy dla mojej trasy, który również musiał być dołączony.
Html.ActionLink(article.Title, "Login", "Item", new { id = article.ArticleID, title = article.Title }, null)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:03:07
Możesz spojrzeć na RouteLink()
metoda.Ten pozwala określić wszystko (z wyjątkiem tekstu łącza i nazwy trasy) za pomocą słownika.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-08 15:03:59
Myślę, że Joseph przewrócił kontroler i akcję. Najpierw następuje akcja, a następnie kontroler. Jest to nieco dziwne, ale sposób, w jaki wygląda podpis.
Tak dla wyjaśnienia, to jest wersja, która działa (adaptacja przykładu Józefa):
Html.ActionLink(article.Title,
"Login", // <-- ActionMethod
"Item", // <-- Controller Name
new { id = article.ArticleID }, // <-- Route arguments.
null // <-- htmlArguments .. which are none
)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-08 15:04:16
Html.ActionLink(article.Title, "Login/" + article.ArticleID, 'Item")
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-10-14 13:47:11
Co z tym
<%=Html.ActionLink("Get Involved",
"Show",
"Home",
new
{
id = "GetInvolved"
},
new {
@class = "menuitem",
id = "menu_getinvolved"
}
)%>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-03 18:41:56
Jeśli chcesz przejść wszystkie fantazyjne spodnie, oto jak możesz go rozszerzyć, aby móc to zrobić:
@(Html.ActionLink<ArticlesController>(x => x.Details(), article.Title, new { id = article.ArticleID }))
Musisz umieścić to w System.Web.Mvc
przestrzeni nazw:
public static class MyProjectExtensions
{
public static MvcHtmlString ActionLink<TController>(this HtmlHelper htmlHelper, Expression<Action<TController>> expression, string linkText)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);
var link = new TagBuilder("a");
string actionName = ExpressionHelper.GetExpressionText(expression);
string controllerName = typeof(TController).Name.Replace("Controller", "");
link.MergeAttribute("href", urlHelper.Action(actionName, controllerName));
link.SetInnerText(linkText);
return new MvcHtmlString(link.ToString());
}
public static MvcHtmlString ActionLink<TController, TAction>(this HtmlHelper htmlHelper, Expression<Action<TController, TAction>> expression, string linkText, object routeValues)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);
var link = new TagBuilder("a");
string actionName = ExpressionHelper.GetExpressionText(expression);
string controllerName = typeof(TController).Name.Replace("Controller", "");
link.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));
link.SetInnerText(linkText);
return new MvcHtmlString(link.ToString());
}
public static MvcHtmlString ActionLink<TController>(this HtmlHelper htmlHelper, Expression<Action<TController>> expression, string linkText, object routeValues, object htmlAttributes) where TController : Controller
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);
var attributes = AnonymousObjectToKeyValue(htmlAttributes);
var link = new TagBuilder("a");
string actionName = ExpressionHelper.GetExpressionText(expression);
string controllerName = typeof(TController).Name.Replace("Controller", "");
link.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));
link.MergeAttributes(attributes, true);
link.SetInnerText(linkText);
return new MvcHtmlString(link.ToString());
}
private static Dictionary<string, object> AnonymousObjectToKeyValue(object anonymousObject)
{
var dictionary = new Dictionary<string, object>();
if (anonymousObject == null) return dictionary;
foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(anonymousObject))
{
dictionary.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(anonymousObject));
}
return dictionary;
}
}
Obejmuje to dwa przesłonięcia dla Route Values
i HTML Attributes
, również wszystkie twoje poglądy powinny dodać: @using YourProject.Controllers
lub możesz dodać je do swojego web.config <pages><namespaces>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-26 21:29:22
Użyj nazwanych parametrów, aby uzyskać najlepszą czytelność i uniknąć nieporozumień:
@Html.ActionLink(
linkText: "Click Here",
actionName: "Action",
controllerName: "Home",
routeValues: new { Identity = 2577 },
htmlAttributes: null)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-14 11:56:04
Z MVC5 zrobiłem to tak i jest to w 100% działający kod....
@Html.ActionLink(department.Name, "Index", "Employee", new {
departmentId = department.DepartmentID }, null)
Możecie się z tego wywnioskować...Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-25 13:36:17
Użycie tego typu:
@Html.ActionLink ("Strona Główna","Indeks","Strona Główna")
Strona główna: nazwa tekstu Index: Action View Home: HomeController
Base Use ActionLink
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>_Layout</title>
<link href="@Url.Content("~/Content/bootsrap.min.css")" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<div class="col-md-12">
<button class="btn btn-default" type="submit">@Html.ActionLink("AnaSayfa","Index","Home")</button>
<button class="btn btn-default" type="submit">@Html.ActionLink("Hakkımızda", "Hakkimizda", "Home")</button>
<button class="btn btn-default" type="submit">@Html.ActionLink("Iletişim", "Iletisim", "Home")</button>
</div>
@RenderBody()
<div class="col-md-12" style="height:200px;background-image:url(/img/footer.jpg)">
</div>
</div>
</body>
</html>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-17 20:29:02