1

I have an area called MyArea and it's registered like so:

context.MapRoute(null, "MyArea", new { controller = "MyAreaController", action = "Index" });

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

//Units
context.MapRoute(null, "MyArea/properties/edit/{propertyId}/units/{unitId}", new { action = "Unit", propertyId = 1, unitId = 1 });

It should work that one property has many units, so I would like my url to look something like this:

http://localhost:50182/myarea/properties/edit/4/units/1

The code i use for the Html.ActionLink looks like:

@Html.ActionLink("Add new Unit", "Unit", "Unit", new { propertyId = 1, unitId = 1 })

I have an Unit controller with an action called Unit. Pleas help, what am i missing?

Thanks!!

1

1 Answer 1

1

You say "I have an Unit controller with an action called Unit. Pleas help, what am i missing?"

and your route mapping is currently ...

 context.MapRoute(null, "MyArea/properties/edit/{propertyId}/units/{unitId}", new { action = "Unit", propertyId = 1, unitId = 1 });

How would you expect MVC to know what controller to use for that route? You need to specify controller = "Unit"

Update

Switch the order of

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

   //Units
   context.MapRoute(null, "MyArea/properties/edit/{propertyId}/units/{unitId}", new { action = "Unit", propertyId = 1, unitId = 1 });

in your route registration. Otherwise, something that should map to the second route will be intercepted by the first.

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

7 Comments

I did try that but the the url then goes to: localhost:50182/myarea/properties/edit
Swopped it around, but the url this goes to localhost:50182/myarea/properties/edit
and, you set the controller = "Unit" ? You must at least be getting a querystring...
yip it looks like this: context.MapRoute(null, "myarea/properties/edit/{propertyId}/units/{unitId}", new { controller="Unit", action = "Unit", propertyId = 1, unitId = 1 });
My Html.Actionlink looks like this: Is it correct? @Html.ActionLink("Add new Unit", "Unit", "Unit", new { propertyId = 1, unitId = 1 })
|

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.