0

I have a controller called TaskListsController with an action called Edit.

The Edit action accepts a nullable int parameter. If it receives null, it adds a TaskList. If it receives an int, it edits the TaskList with the same ID. The logic is almost identical in each case.

I would like to configure my routing in such a way that the URL 'TaskLists/Add' maps to the Edit action with null as the parameter, and the URL 'TaskLists/Edit/{id}' maps to Edit and passes it the ID in the URL.

I can get this to work from the point of view of entering URLs in the browser and having them routed to the action correctly, but where it's falling down is where the system generates URLs from Html.ActionLink.

I am generating 'Add' links using:

Html.ActionLink("Add task list", "Edit", "TaskLists")

And I'm generating 'Edit' links using:

Html.ActionLink(taskList.Name, "Edit", new { Id = taskList.Id })

..where taskList is an instance of the TaskList class.

If I use this routing:

routes.MapRoute(
                "TaskLists/Add", // Route name
                "TaskLists/Add", // URL with parameters
                new { controller = "TaskLists", action = "Edit" });

            routes.MapRoute(
                "TaskLists/Edit/{id}", // Route name
                "TaskLists/Edit/{id}", // URL with parameters
                new { controller = "TaskLists", action = "Edit", id = UrlParameter.Optional });

...the 'Add' link is generated correctly ('TaskLists/Add') but the 'Edit' link comes out 'TaskLists/Add?Id=1'.

If I put the routing commands the other way around:

routes.MapRoute(
                "TaskLists/Edit/{id}", // Route name
                "TaskLists/Edit/{id}", // URL with parameters
                new { controller = "TaskLists", action = "Edit", id = UrlParameter.Optional });

            routes.MapRoute(
                "TaskLists/Add", // Route name
                "TaskLists/Add", // URL with parameters
                new { controller = "TaskLists", action = "Edit" });

...then the 'Edit' links are generated correctly ('TaskLists/Edit/x'), but the 'Add' links comes out 'TaskLists/Edit'.

Is there a way I can have my cake and eat it?

1 Answer 1

2

Using named routes (Html.RouteLink("linkText", "routeName")) could be a cleaner way of defining this, as then you're dealing with a clean route in the view also. This would also mean that you will hit the correct route every time without worry.

Update the Name parameter on your routes accordingly, then add the following to your view:

Html.RouteLink("Add task list", "NewTaskList")

and

Html.RouteLink(taskList.Name, "EditTaskList", new { Id = taskList.Id })

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

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.