3

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!

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the input, but I receive the same result, it still redirects to the ManagementController's Index result, not the PropertyController's Edit actionresult
3

In your route you defined a parameter called propertyId not id:

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

Try this:

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

2 Comments

I've tested this route and it works: localhost/management/properties/edit/1
ok, by following both suggestions, e.g. specifying the Area, and changing Id to propertyId...it works!! Thanks a lot!
1

I'd suggest using constraints. For example my default route in the Global.asax is as follows:

routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { typeof(MyProject.Controllers.HomeController).Namespace });

Then in my area registration:

context.MapRoute(
"Search_default",
"Search/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { typeof(MyProject.Areas.Search.Controllers.HomeController).Namespace }
            );

This means I get {AppName}/ as well as {AppName}/Search/Home and both work a treat.

Comments

0

From any Area to Home do following

Url.Action("Details", "User", new { @id = entityId, area = "" })

From Home to any Area

Url.Action("Details", "Order", new { @id = entityId, area = "Admin" })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.