ActionLink can't access root folder - MVC 3 - asp.net-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.

Related

Adding a class to Html.ActionLink - ASP.NET MVC 3

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"})

ASP.NET MVC Areas Naviation

I have a MVC web application with a logout form like this:
#using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { area = "", id = "logoutForm" }))
{
#Html.AntiForgeryToken()
Log off
}
The problem is that if I have navigated to one of the areas and I click the LogOut button the action does not work.
Does anyone know how to make the above code work with the default area?
Thanks
Gerry
Got it:
#using (Html.BeginForm("LogOff", "Account", new { area = "" }, FormMethod.Post, new { id = "logoutForm" }))
This works!!!!!

ajax.actionlink post form not showing ckeditor

After Using the Ajax.ActionLink As
#Ajax.ActionLink("Edit", "AddEdit", new { #id = id, #recId = item.EncyclopediaID }, new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "listForm" }, new { #class = "edit_icon", #title = "edit this item" })
And When Controller Go to AddEdit Page There i have Uploaded the File and want to show the Ckeditor.
So i use
#using (Html.BeginForm("AddEdit", "Encyclopedia", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
/////////other code//////////////////
#Html.EditorFor(model => model.Description,"CKEditor")
}
Now if i use to show the #Html.ActionLink instead of #Ajax.Actionlink Ckeditor Shows perfectly and in Ajax call it showed like a textarea.
Please Help.
this happens because resource files required for ckeditor to work correctly (eg: css , javascript) cannot be downloaded with an ajax call.
try referencing the required resource files in layout view page and try again.
Edit :
maybe you have placed integration code in document.ready function, thus after ajax request completed and you markup changed, the new markup (eg:your new input element) is not configured as ckeditor input.so try calling ckeditor integration code after ajax success.

MVC3 #Html.ActionLink displays Text together with the address

I do not know what I have done wrong but all my #Html.ActionLinks are displaying the expected text together with hyperlink instead of the text alone.
#Html.ActionLink("About", "About", "Home")
displays as
About(Home/About)
instead of
About
In the views,
#Html.ActionLink("Edit", "Edit", new { id = item.Id })
displays
Edit(Home/Edit/4)
instead of the text
Edit
Any clues would be appreciated.
You might have an extension or DisplayTemplate that would change the default behaviour.
It looks like your second example is missing a parameter (the last null for html attributes, I also included the Controller here)
#Html.ActionLink("Edit", "Edit", "Home", new { id = item.Id }, null)

MVC Html.ActionLink not passing querystring properly

This seems like it should be pretty straight forward, but I'm apparently confused.
I have a List view that displays a paged list. At the bottom I have a set of actionlinks:
<%= Html.ActionLink("First Page", "List", new { page = 1} ) %>
<%= Html.ActionLink("Prev Page", "List", new { page = Model.PageNumber - 1 }) %>
<%= Html.ActionLink("Next Page", "List", new { page = Model.PageNumber + 1 }) %>
<%= Html.ActionLink("Last Page", "List", new { page = Model.LastPage } )%>
I'm using the basic default routes setup, except with "List" substituted for "Index":
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
The problem is that the ActionLink helpers are generating links of the form:
http://localhost:2083/Retrofit?page=2
rather than
http://localhost:2083/Retrofit/?page=2
(with the trailing slash after the controller name & before the query string). When the first URL is routed, it completely loses the query string - if I look at Request.QueryString by the time it gets to the controller, it's null. If I enter the second URL (with the trailing slash), it comes in properly (i.e., QueryString of "page=2").
So how can I either get the ActionLink helper to generate the right URL, or get the Routing to properly parse what ActionLink is generating?
Thanks.
You need to choose one of two actions:
Modify your route to take a page parameter instead of the default id, and modify the signature of your action method accordingly.
Modify the calls to Html.AcitonLink() so they name the parameter id - that is, change new { page = ... } to new { id = ... }.
This is an old question but for others I thought I would add the answer you are looking for is add both to the routeValues parameter like
Html.ActionLink("Last Page", "List", new { id = Model.ID, page = Model.LastPage } )

Resources