How can I include a bookmark/fragment in an ActionLink? [duplicate] - asp.net-mvc-3

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Including an anchor tag in an asp.net mvc Html.ActionLink
The code : #Html.ActionLink("Link", "Action", "Controller", new { id = Id } )
For the moment I can generate links like this :
http://mywebsite/Controller/Action/Id
I would like to generate a link like this :
http://mywebsite/Controller/Action/Id#divId
But I can't edit the route/create another route.
What is the best solution?

Just use the proper overload of the ActionLink helper:
#Html.ActionLink(
linkText: "Link",
actionName: "Action",
controllerName: "Controller",
protocol: null,
hostName: null,
fragment: "divId",
routeValues: new { id = Id },
htmlAttributes: null
)
will generate:
Link

Related

How do you set the linktext on url in controller

is there a way to create an action link in my controller and set the link text? Can't find an overload...
Url.Action("Edit", "Controller", new { id = myId });
To create a link in controller use Helpers like this:
string link = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, System.Web.Routing.RouteTable.Routes, "My link", "Root", "About", "Home", null, null);
Here you go: ActionLink Method
#Html.ActionLink("Edit", "Edit", "Controller", new { id = myId })

Html.ActionLink helper creating incorrect link when trying to apply ID

I'm getting an odd result when trying to assign an ID attribute to an [a] tag created via the Razor #Html.ActionLink helper
My original code was this:
#Html.ActionLink("Create New Order", "Index", "NewOrder")
This works correctly and creates a link as http://www.mysite.com/NewOrder but I want to add an ID tag to my element.
I then tried this
#Html.ActionLink("Create New Order", "Index", "NewOrder", new {#id = "orderlink"})
This creates a link of http://www.mysite.com/Orders?Length=8
UPDATE:
When I use the solution provided,
#Html.ActionLink("Create New Order", "Index", "NewOrder", new { #id = "orderlink" }, null)
I get a link that looks like this:
http://www.mysite.com/NewOrder/Index/orderlink
I don't want the orderlink added to my link/route. I want it added to the [a] tags attributes. See the top of the question.
I want to get this:
<a id="orderlink" href="/tcap/NewOrder/Index" >Create New Order</a>
Try this overload.
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
Object routeValues,
Object htmlAttributes
)
So your code will be
#Html.ActionLink("Create New Order", "Index", "NewOrder",
new {#id = "orderlink"},null)
EDIT : As per your comment/ updated question
Use this overload
#Html.ActionLink("Create New Order", "Index", "NewOrder")
this will give you mysite.com/NewOrder/Index url
EDIT2 :
If you want, <a id="orderlink" href="/tcap/NewOrder/Index" >Create New Order</a>
Use this, pass HTML attributes as the fifth parameter of this overload
#Html.ActionLink("Create New Order", "Index",
"NewOrder",null,new {#id="orderlink"})
answered this the other day here:
ASP.NET MVC basic routing with parameters
in a nutshell, add a final null parameter to the overload:
#Html.ActionLink("Create New Order", "Index", "NewOrder",
new {#id = "orderlink"}, null)
Adding the null as the final param (htmlAttributes) is all that's missing for you in this scenario (there are 9 overloads for Html.ActionLink, so it's VERY easy to miss the correct implementation).

ActionLink can't access root folder - MVC 3

I have an .Net MVC 3 web application that has the following structure
root
Views
Account
LoginPage.aspx
Controllers
AccountController
Areas
Course
Index.aspx
Imagine I am currently at the Index.aspx page in the Course area, and I would have a button that would forward me to the LoginPage.aspx
In a ASP.Net MVC I would call http://localhost/Account/Login that it would lead me to the correct page. If I just write it down on the browser it works!
But I would like to use the HTML Helper ActionLink, so I tried:
<%: Html.ActionLink("Log on", "Login", "Account", routeValues: null, htmlAttributes: new { id = "logonLink", data_dialog_title = "Logon" })%>
It get the relative path: http://localhost/Course/Account/Login
I tried also:
<%: Html.ActionLink("Log on", "Login", "../Account", routeValues: null, htmlAttributes: new { id = "loginLink", data_dialog_title = "Login" })%>
I got the error message: Cannot use a leading .. to exit above the top directory.
I also tried the relative path:
<%: Html.ActionLink("Log on", "Login", "~/Account", routeValues: null, htmlAttributes: new { id = "loginLink", data_dialog_title = "Login" })%>
And it lead me to:
http://localhost/Course/~/Account/Login
I would really appreciate how to find a solution for this problem.
Try this:
<%: Html.ActionLink("Log on", "Login", "Account", new { area = "" } , new { id = "logonLink", data_dialog_title = "Logon" })%>
The key is the area = "" bit. When using Url.Action or Html.ActionLink with areas, if you don't specify the area route value, MVC will only look for a match in the current area.
It get's even more important when using partial views/templates, since they can be rendered in Views in different areas.
So if using areas, get into the habit of always specifying the area route value, unless using Url.RouteUrl or Html.RouteLink.

Html.ActionLink equivalent of a specific URL format

I want to use Html.ActionLink to return an anchor tag with href like /Product/22/Product-Name, where Product is my controller name and 22 is my id and Product-Name is my title.
In my global.asax, I have set the following route :
routes.MapRoute(
"ProductRoute", // Route name
"Product/{id}/{title}", // URL with parameters
new { controller = "Product", action = "Index", id = UrlParameter.Optional });
I tried using following code
#Html.ActionLink(model.Name, null, "Product",
new { id = model.Id, title = model.Name.Replace(" ","-") }, null)
but it yields Product/Browse/22?title=Product-Name. This code is in Browse View returned by Browse action of Catalog controller and that's why it picks up the Browse in between.
Strangely, the same code works fine in the Index View returned by Index action of the Home Controller. What's the reason for this difference? . Am I missing something or this is a bug in Asp.net MVC3.
What Should I do to achieve my stated URL format. For the meantime, I am using this code
#product.Name
make sure you have defined the custom route before the default route and try
#Html.ActionLink(model.Name,
"Index",
new {controller="Product", id = model.Id, title = model.Name.Replace(" ","-") }, null)
the first parameter is the DisplayName, second the ActionResult's name and then the RouteValues

How can I add an anchor tag to my URL?

MVC 3.net I want to add an anchor to the end a url.
I tried to include an anchor query string but the hash '#' changes to %23 or something like that in the url.
Is there a way of working around this?
There is an overload of the ActionLink helper that allows you to specify the fragment:
#Html.ActionLink(
"Link Text", // linkText
"Action", // actionName
"Controller", // controllerName
null, // protocol
null, // hostName
"fragment", // fragment
new { id = "123" }, // routeValues
null // htmlAttributes
)
will produce (assuming default routes):
Link Text
UPDATE:
and if you wanted to do this within a controller action performing a redirect you could use the GenerateUrl method:
public ActionResult Index()
{
var url = UrlHelper.GenerateUrl(
null,
"Action",
"Controller",
null,
null,
"fragment",
new RouteValueDictionary(new { id = "123" }),
Url.RouteCollection,
Url.RequestContext,
false
);
return Redirect(url);
}

Resources