Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor actionlink autogenerating ?length=7 in URL?

I have the link below on a razor page:

@Html.ActionLink("Create New Profile", "Create", "Profile", new { @class="toplink" }) 

It appears in thes source of view page as shown below:

<a href="/admin/profile/create?length=7" class="toplink">Create New Profile</a> 

When I click on the link the URL is like this:

http://localhost:54876/admin/profile/create?length=7 

I don't want ?length=7. Why is this auto generated?

like image 703
Pirzada Avatar asked Dec 05 '10 07:12

Pirzada


People also ask

What is difference between HTML ActionLink and URL action?

Yes, there is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.

What is HTML ActionLink ()?

Html. 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.

How do I transfer my ActionLink model to my controller?

You'll have to serialize your model as a JSON string, and send that to your controller to turn into an object. Please don't format with snippets when the language used is not displayable by them. It doesn't improve the answer and makes your answer more cluttered. Regular code blocks will work fine.

How do I post on ActionLink?

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.


1 Answers

The ActionLink override you are using matches to the (string linkText, string actionName, Object routeValues, Object htmlAttributes) override. So your "Profile" value is being passed to the routeValues parameter. The behavior of this function with respect to this parameter is to take all public properties on it and add it to the list of route values used to generate the link. Since a String only has one public property (Length) you end up with "length=7".

The correct overload you want to use is the (string linkText, string actionName, string controllerName, Object routeValues, Object htmlAttributes) and you call it loke so:

@Html.ActionLink("Create New Profile", "Create", "Profile", new {}, new { @class="toplink"}) 
like image 80
marcind Avatar answered Sep 23 '22 02:09

marcind