Asp.net-mvc – ASP.NET MVC 3 – Area URL Routing Problem

asp.net-mvc

I have an ASP.Net MVC 3 application. With 2 Areas:

  • Auth – handles all the authentication etc
  • Management – area for property management

In the Management Area I have a ManagementController and a PropertyController. The ManagementController does nothing, it only has a simple Index ActionResult than return a view.

The PropertyController has an Index ActionResult that returns a view with a list of properties as well as an Edit ActionResult that takes a propertyId as parameter. In the Property Index view i have a grid with the list of properties and an option to edit the property with the following code:

@Html.ActionLink("Edit", "Edit","Property", new { id = item.PropertyId })

In theory this should redirect to the Edit ActionResult of my Property Controller,however it redirects to the Index ActionResult of my ManagementController. My ManagementAreaRegistration file looks like this:

context.MapRoute(null, "management", new { controller = "management", action = "Index" });
context.MapRoute(null, "management/properties", new { controller = "Property", action = "Index" });
context.MapRoute(null, "management/properties/Edit/{propertyId}", new { controller = "Property", action = "Edit" });

And my global.asax's RegisterRoutes like this:

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

What am I missing, why would it redirect to the wrong controller and action?
Thanks in Advance!

Best Answer

You might need to specify the area in your route values parameter:

@Html.ActionLink("Edit", "Edit", "Property", new { id = item.PropertyID, area = "Management" }, new { })

Based on the constructor used for this call, you need to specify the last parameter, htmlAttributes, which, for your purposes, would be empty.

Related Topic