I need to add a class to this link:
#Html.ActionLink("Sign Out", "LogOff", "Account")
But when I do this:
#Html.ActionLink("Sign Out", "LogOff", "Account",new{#class="btn blue"})
The link points to the Home controller, not the Account controller thus throwing a 404.
/Home/LogOff?Length=7
What am I doing wrong?
Thanks
Try using the proper overload of the ActionLink helper (yeah there are gazillions of overloads):
#Html.ActionLink(
"Sign Out", // linkText
"LogOff", // actionName
"Account", // controllerName
null, // routeValues
new { #class = "btn blue" } // htmlAttributes
)
whereas you were using:
#Html.ActionLink(
"Sign Out", // linkText
"LogOff", // actionName
"Account", // routeValues
new { #class = "btn blue" } // htmlAttributes
)
See why your code is not working?
Yeah, Microsoft did a hell of a mess with those overload and if you are not careful you get caught into the trap.
Solution: read MSDN or use Visual Studio Intellisense (F12 while your cursor is over the ActionLink helper).
For that reason I prefer to write it in a mode explicit manner using C# 4.0 named parameters:
#Html.ActionLink(
linkText: "Sign Out",
actionName: "LogOff",
controllerName: "Account",
routeValues: null,
htmlAttributes: new { #class = "btn blue" }
)
When calling the ActionLink, there are several overloaded functions to call this. The one you want to use is Html.ActionLink("Link Text", "Action Name", Controller", "Route Values", HTML Attributes")
So something like this: #Html.ActionLink("Sign Out", "LogOff", "Account", null, new{#class="btn blue"})
Related
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 })
I have an code segment of view .cshtml page like :
#Html.ActionLink("Add Charts of Account", "Save")
Run mode i can see following HTML Code:
Add Charts of Account
But i want to following html code :
<a id="lblAddNode" href="/ChartsOfAccount/Save">Add Charts of Account</a>
is it possible? If possible please explain with example code.
There's an overload of the ActionLink helper which allows you to specify routeValues and htmlAttributes:
#Html.ActionLink(
"Add Charts of Account", // linkText
"Save", // actionName
null, // routeValues
new { id = "lblAddNode" } // htmlAttributes
)
I have some Razor code in a view that is supposed to route through to a different part of my model:
#Html.ActionLink("Edit", "Edit", "Journal", new { id = item.JOURNAL.REF_ID })
but when I look at the HTML that is emmitted, it is does not relect what I have written:
Edit
How can I stop this from happening?
That's because you are using the wrong overload. It should be like this:
#Html.ActionLink("Edit", "Edit", "Journal", new { id = item.JOURNAL.REF_ID }, null)
Let's see why you are using the wrong overload. Let's break down what you wrote:
#Html.ActionLink(
"Edit", // linkText
"Edit", // actionName
"Journal", // routeValues
new { id = item.JOURNAL.REF_ID } // htmlAttributes
)
See the problem?
And now let's break down the correct way:
#Html.ActionLink(
"Edit", // linkText
"Edit", // actionName
"Journal", // controllerName
new { id = item.JOURNAL.REF_ID }, // routeValues
null // htmlAttributes
)
See the difference?
I would recommend you reading very carefully the documentation and the different available overloads of the ActionLink helper as well as the exact significance of their parameters.
I've a view page in which there's an HTML ActionLink. Now on click of that action link, i want to open a new popup window without closing the previous one. In this new popup window i want to show my existing view page. please provide me a better solution.
Specify the HTML 'target' attribute using the htmlAttributes parameter. In Razor:
#Html.ActionLink("New Window", "Index", null, new { target= "_blank" })
In ASPX syntax:
<%= Html.ActionLink("New Window", "Index", null, new { target= "_blank" }) %>
Could not get the answers on here to work. I had to use
<%= Html.ActionLink("New Window", "Index", null, new { .target= "_blank" }) %>
This worked for me. It was just the silly little period at the beginning of target that was missing.
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.