HTML.ActionLink method
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.
<a href="/Item/Login/5">Title</a>
This will give you the following html output, assuming:
article.Title = "Title"
article.ArticleID = 5
- 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
);
How to correctly use Html.ActionLink with ASP.NET MVC 4 Areas
I hate answering my own question, but @Matt Bodily put me on the right track.
The @Html.Action
method actually invokes a controller and renders the view, so that wouldn't work to create a snippet of HTML in my case, as this was causing a recursive function call resulting in a StackOverflowException. The @Url.Action(action, controller, { area = "abc" })
does indeed return the URL, but I finally discovered an overload of Html.ActionLink
that provided a better solution for my case:
@Html.ActionLink("Admin", "Index", "Home", new { area = "Admin" }, null)
Note: , null
is significant in this case, to match the right signature.
Documentation: @Html.ActionLink (LinkExtensions.ActionLink)
Documentation for this particular overload:
LinkExtensions.ActionLink(Controller, Action, Text, RouteArgs, HtmlAttributes)
It's been difficult to find documentation for these helpers. I tend to search for "Html.ActionLink" when I probably should have searched for "LinkExtensions.ActionLink", if that helps anyone in the future.
Still marking Matt's response as the answer.
Edit: Found yet another HTML helper to solve this:
@Html.RouteLink("Admin", new { action = "Index", controller = "Home", area = "Admin" })
C# MVC 5 Html.ActionLInk
I think the proper URI should be (/Customer/CustomerView/2) and not (/CustomerController/CustomerView/2).
below is correct line of code.
@Html.ActionLink(customer.Name, "CustomerView", "Customer", new { id = customer.Id }, null)
How do I make an MVC ActionLink call a POST method?
When creating a link to a controller action in ASP.NET MVC, using the generic ActionLink method is preferable, because it allows for strongly typed links that are refactoring friendly.
Default: ActionLink
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
Using Button:
<input type="button" title="Delete" value="D" onclick="location.href='@Url.Action("Delete", "movies", new { id = item.ID })'" />
Using Image:
<a href="@Url.Action("Delete", "movies", new { id = item.ID })" title="Edit">
<img src="../../Content/Images/Delete.png" />
</a>
ASP.Net MVC5 Html.ActionLink to index method in different controller
I believe your call is matching the wrong overload of the helper method.
As written, it will match this signature:
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes)
Notice there is no controller in there.
Try this instead:
@Html.ActionLink("View Staff", "Index", "Staff" , new { id = item.UnitCode }, null)
Which should match the right signature with controller:
public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
Html.ActionLink extension
The function you are looking for is:
HtmlHelper.AnonymousObjectToHtmlAttributes()
https://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper.anonymousobjecttohtmlattributes(v=vs.118).aspx
Here is one version of the ModalLink Extension:
public static MvcHtmlString ModalLink(this HtmlHelper htmlHelper, string title, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
// Add 'show-in-modal' class here
// Add 'data-title' attribute here
var htmlAttr = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
const string classKey = "class";
const string titleKey = "data-title";
const string classToAdd = "show-in-modal";
if (htmlAttr.ContainsKey(classKey) == true)
{
htmlAttr[classKey] += " " + classToAdd;
}
else
{
htmlAttr.Add(classKey, classToAdd);
}
if (htmlAttr.ContainsKey(titleKey) == true)
{
htmlAttr[titleKey] = title;
}
else
{
htmlAttr.Add(titleKey, title);
}
return htmlHelper.ActionLink(linkText, actionName, controllerName, new RouteValueDictionary(routeValues), htmlAttr);
}
Html.ActionLink onclick method equivalent for Url.Action
Try this...
<a href="@Url.Action("Action","Controller", new {refNumber = item.RefNumber, null})" onclick="return confirm('are you sure?');">Delete</a>
As mentioned in the comments, I'd highly recommend changing your action to a POST. Can read more about that here.
ASP.NET MVC : Actionlink does not navigate to correct action method of same name but different parameters
An ActionLink
is rendered as an <a href="....">
HTML element and therefore is always a GET
request and thus always goes to the action method decorated with [HttpGet]
annotation - it does NOT look at any parameter list or anything like that.
If you want to call the POST
action method, you need to have a HTML form and a submit button on that form - that will be a POST
request to be handled by the method decorated with the [HttpPost]
annotation ...
ActionLink with parameter in another controller MVC5
There is another overload of ActionLink helper which lets you specify the controller name, action name and the route values.
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
object routeValues,
object htmlAttributes
)
So if you want to render a which points to Table2/Action1
, you ca use that overload
@Html.ActionLink("Edit", "Action1","Table2", new { ParentID = 100 },null)
You can change hardcoded 100 with your actual value.
Using ASP.Net MVC Razor - @Html.ActionLink
You are specifying the controller and action both in the same parameter using the overload which takes just action method name which is not correct in this case as you want to call action method on a different controller class.
You actually need to specify action method name and controller name using the following overload method:
@Html.ActionLink("Order", "ProductList","Products",
new { id = Model.ID },
new { @class = "link-button btn-primary" })
and in controller class you will have to add an action method for it:
public class ProductsController : Controller
{
................
................
// other action methods here
public ActionResult ProductsList(int id)
{
// your code goes here and finaly returns view
// var model = new Model();
return View(model)
}
}
Related Topics
Setting Authorization Header of Httpclient
Entity Framework - Include Multiple Levels of Properties
Error: "An Object Reference Is Required For the Non-Static Field, Method or Property..."
Compression/Decompression String With C#
How to Get the List of Open File Handles by Process in C#
Floating Point Comparison Functions For C#
Unit Testing Private Methods in C#
C# Data Connections Best Practice
Difference Between Covariance & Contra-Variance
Serialport Not Receiving Any Data
Is Task.Result the Same as .Getawaiter.Getresult()