ASP.NET MVC ActionLink outside area - asp.net-mvc

A simple task in MVC, sometimes becomes a hard challenge.
Well,i have an Area called Admin. I've a page named "Forbidden" inside the Shared's directory in this area.
The goal is simple: I need to create an Html.ActionLink that generates a link to return to Home page which is OUTSIDE the Admin area.
So i try,<%= Html.ActionLink("Back","Index",new {controller="Home"})%>,and its generate :
http://localhost/Admin/Home/Index
Its wrong!I want:
http://localhost/Home/Index
How can i create a link from an area to the default controllers structure?

Try this :
<%= Html.ActionLink("Back", "Index", "Home", new { area = "" }, null) %>
When using Areas, you should always specify the area your are calling in your ActionLinks by adding a route value as above, If the link is outside the area (as in your case), just use an empty parameter for the area.
There's a nice extension that i find essential in any ASP.NET MVC project (T4MVC). It makes your ActionLinks look much cleaner and it protects them against errors.
So the above code will look something like this :
<%= Html.ActionLink("Back", MVC.Home.Index()) %>
and when using an area :
<%= Html.ActionLink("Some Link", MVC.Admin.SomeController.SomeAction()) %>
It's a part of the MvcContrib project on codeplex here
You should consider using it.

Related

T4MVC #Url.Action(MVC.Controller.Action()) Renders "?Area=" Parameter in QueryString

I am rendering a menu from a Partial Action directly to the layout, using:
#Html.Action(MVC.Menu.Index())
This action, determines which Menu partial to render. For instance, a public menu partial. Within these partials, I am also using T4MVC to render the links:
<ul id="navHolder">
<li class="level1">
<ul class="mainMenu">
<li><b>#Html.ActionLink("Welcome", MVC.Home.Index())</b>
...
For some reason, the Urls rendered by T4MVC include "?Area=" at the end:
<ul id="navHolder">
<li class="level1">
<ul class="mainMenu">
<li><b>Welcome</b>
...
I have NO areas in my project and I have turned the "IncludeAreasToken" setting to false. Oddly, this only happens if I render the partial using "#Html.Action" -- if I pull it in as "#Html.Partial" the parameter isn't rendered and the link is clean and correct. (I don't want to render it as a partial though, so please don't offer that as a suggestion ;)
Anyone out there run into this before?
I solve this issue in a very easy way, simply by adding to all routes that are not in area empty area route like this:
routes.MapRoute(
"Default",
"{controller}/{action}/{i​d}",
new { controller = "Home", action = "Index", area = "", id = UrlParameter.Optional });
Something strange is going on here, and I wonder if there is some kind of MVC bug at the root. Even without using T4MVC, this happens if you write:
#Html.ActionLink("Welcome", "Index", "Home", new { Area = "" }, null)
In a regular view, this doesn't generate the bogus ?Area=, while in a Html.Action call it does. I need to ask someone on the team.
For now, you can workaround by deleting this line (around line 310) in t4mvc.tt:
<# if (MvcVersion >= 2) { #>result.RouteValueDictionary.Add("Area", area ?? "");<# } #>
Copied from workitem 7154 comments is a solution provided by #DavidEbbo:
A simpler workaround is to add a bogus area to your site. e.g.\n\n-
Right click Project and choose Add / Area. Name it 'Dummy' (or
whatever)\n- You can delete everything in there except for the
DummyAreaRegistration.cs file
Make sure you have the AreaRegistration.RegisterAllAreas(); call in your Global.asax
This also works with attribute routing in place.

Building pagination in MVC: Is there anyway to get something like an Html.HyperLinkFor

What I mean by that is a type element that will have a label based on a value in my viewModel and also be able to submit that value back up to the viewModel so it can grab new results based on Current Page and Page Size. This is for creating a gridview in MVC that supports pagination.
None of the examples I've seen of MVC so far have had anything resembling a gridview. It's important that I create my own paging and not use any built in paging mechanisms or third party controls or html helpers or the like
I'd go with ActionLink. To build Urls you can either use Url.Action or Html.BuildUrlFromExpression(c => c.conTrollerAction)
If you use T4MVC, you will get some nice helpers that do exactly what you are looking for.
Something along the lines of:
<a href="<%: Url.Action(MVC.MyController.MyAction(Model.ActionMethodParam1, Model.ActionMethodParam2)) %>">
You can also use the Html.ActionLink helper:
<%: Html.ActionLink("Link Name", MVC.MyController.MyAction(Model.ActionMethodParam1, Model.ActionMethodParam2)) %>
T4MVC is a great little library that I use in every MVC app.

How do I use an ActionLink with the RouteValueDictionary?

I'm trying to make my URL look like this:
http://domain.com/controller/action/123/SomeTextForSEO
I tried using an Action Link, but that appends ?company=SomeTextForSEO instead of the company name after a slash.
<%: Html.ActionLink("DomainList", "Index", "DomainList", new { id = item.CompanyID, company = item.CompanyDisplayName.Trim() }, new object { })%>
and now I think I need to use a RouteValueDictionary but I'm unsure of how to do this in ASP.NET syntax. Can someone point me in the right direction? The MSDN examples are insufficient.
If you go to global.asax.cs and add a new route similar to the existing default route with the pattern
"{controller}/{action}/{id}/{company}"
Before the default one, you should find that the link will generate correctly with your ActionLink call as-is.
I'm on a phone so I'd like to be more verbose (a route restriction would be a good idea to prevent this route interfering with the default), but the HTC keyboard is not code friendly ;)

Asp.Net MVC - RenderPartial - Create in a List view

I got a page that lists all my articles (Articles/List.aspx).
I also got a control that create article (Article/Create.ascx).
I will like that my List.aspx page that's render the Create.ascx to be able to create article.
I know that in MVC, the preferred approach is one page by action. But in this case I need to do that. It's a design issue and how the client want the Web site to work.
So for now, I got the following code in List.aspx :
<% Html.RenderPartial("Create", new Domain.Models.Article()); %>
That render correctly. But when I hit the create button, it's doesn't go in the Create[post] method of my ArticleController.
Any idea why and how I could resolve that issue ?
If you have problems with the button, it's not going to have anything to do with how you're rendering the user control. We need to see the form markup that the button is inside, that will show what the problem is most likely.
But just for reference, you're probably looking to do something like this:
<% using (Html.BeginForm("Create",
ViewContext.RouteData.Values["Controller"].ToString())) { %>
your control markup here
<% } %>

ASP.NET MVC - Is it possible to generate cross-application action links?

is it possible to generate cross-application action links using the HTML helper class?
I have the following (both are separate VS2008 projects):
http://mainwebsite/
http://mainwebsite/application
I would like to generate a link IN the /mainwebsite/application/ project TO /mainwebsite/. Is this possible? Or should I just hardcode a hyperlink?
addition:
My question also applies vice-versa. So generating a link IN /mainwebsite/ TO /mainwebsite/application/. I already managed to do like so, by simulating the application name as controller name:
<% =Html.ActionLink("ApplicationName","",new With {.Controller = "application" }) %>
Yes, you can do this. The HTML helper generates URLs using the routing system, which is completely ignorant of ASP.NET MVC and your application. If you have a route in your route table for the other application, then you can generate a URL for it using the HTML helper. There is no rule which states that your route table can only contain routes for the current application, although you obviously need to be careful about how you order the routes.

Resources