Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MVC ActionLink

Can anyone explain why the following happens? And how to resolve, Visual Studio 2010 and MVC2

<%= Html.ActionLink("Add New Option", "AddOption", "Product", new { @class = "lighbox" }, null)%>

Results in

/Product/AddOption?class=lightbox

<%= Html.ActionLink("Add New Option", "AddOption", "Product", new { @class = "lighbox" })%>

Results in

/Product/AddOption?Length=7

Thanks

like image 487
LiamB Avatar asked May 24 '10 13:05

LiamB


People also ask

What is ActionLink in MVC?

ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action. Here are some samples of Html.

What is ActionLink in C#?

ActionLink(HtmlHelper, String, String, String, Object, Object) Returns an anchor element (a element) for the specified link text, action, controller, route values, and HTML attributes. C# Copy. public static System.Web.Mvc.

How can we call post method using ActionLink in MVC?

ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to submit (post) Form in ASP.Net MVC 5 Razor. Hence in order to submit (post) Form using @Html. ActionLink, a jQuery Click event handler is assigned and when the @Html.


2 Answers

This is an example of "overload hell" in ASP.NET MVC.

The first code calls the following method:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    Object routeValues,
    Object htmlAttributes
)

whereas the second code calls this one:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    Object routeValues,
    Object htmlAttributes
)

Notice that the string parameter controllerName in the first call is becoming routeValues in the second one. The string value "Product" is being passed to the routed values: the string property Length is used, which has a length of 7 here, hence the "Length=7" you're getting in the route.

Considering the first method, it seems that you've swapped the routeValues and htmlAttributes parameters.

like image 43
Julien Lebosquain Avatar answered Oct 24 '22 14:10

Julien Lebosquain


You're using these respective overloads:

public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
Object routeValues,
Object htmlAttributes
)

From: http://msdn.microsoft.com/en-us/library/dd504972.aspx

public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
Object routeValues,
Object htmlAttributes
)

From: http://msdn.microsoft.com/en-us/library/dd492124.aspx

The first new { @class = "lighbox" } is passed as the routeValues argument when it should be the htmlAttributes argument.

This sort of problem is common with the extension methods used in MVC. In can sometimes help to use named arguments (C# 4.0) to make things more readable:

<%= Html.ActionLink(linkText: "Add New Option", 
   actionName: "AddOption",
   controllerName: "Product", 
   htmlAttributes: new { @class = "lighbox" }, 
   routeValues: null)%>
like image 111
David Neale Avatar answered Oct 24 '22 14:10

David Neale