views:

52

answers:

1

Hi,

I am using MVC 2.

I have 2 controllers called Application and Note. The application is a loan application. A note can be added to an application.

On my Index I have a grid that displays all the applications, and an action column with a link that says "Add Note".

In my Application controller I have action methods for create and edit. In my Note controller I have action methods for create, edit and delete.

I was wondering if the following is possible when the user clicks on the "Add Note" link to go to a URL like:

Application/1/Note/Create

1 is the application ID. Note would be the Note controller, and Create is the action method in the Note controller. Is something like this possible?

I started with the mapping in my global.asax, but not sure if it is correct:

routes.MapRoute(
   null,
   "Application/{ApplicationID}/{controller}/{action}",
   new { controller = "Note", action = "Create" });

How would I create the link in my grid using the action link?

Please could someone advise?

EDIT:

The grid above is on my Index view in my Home direcotry. So on this grid I need to concatenate the link to display as above. I'm struggling to create this link on my Index view. It's not concatenating correctly. Currently I have this:

Html.ActionLink("Add Note", "Create", "Note", new { ApplicationID = c.ApplicationID }, null)

And it is displaying as /Note/Create/?ApplicationID=1

I need it to display as:

/Application/1/Note/Create

Thanks Brendan

+1  A: 

To create a link using this route try using the ActionLink helper method:

<%= Html.ActionLink(
    "Create note", "Create", "Note", new { ApplicationID = "123" }, null
) %>

will produce /Application/123 and navigate to the Create action of NoteController and pass 123 as the applicationID parameter:

public class NoteController : Controller
{
    public ActionResult Create(int applicationID)
    {
        return View();
    }
}

This assumes the routes are registered like the following:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        null,
        "Application/{ApplicationID}/{controller}/{action}",
        new { controller = "Note", action = "Create" });

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}
Darin Dimitrov
Please update above. Thanks.
Brendan Vogt
@Brendan, what is the routes definition order in your global.asax? Do you still have the default route? Where did you put your custom route (before or after the default)?
Darin Dimitrov
@Darin: I added this new route definition above the default one. The link does display /Application/1, but how do I get it to display as /Application/1/Note/Create? I need the /Note/Create also to display, currently it is not.
Brendan Vogt
Why do you need to display it? It will navigate to the proper controller action. It doesn't display it because in the route definition this is the default controller and action name for this route. If you really want it to force to display it in the route definition change the controller name and action name to `foo` for example but I really don't see why would you want to do so.
Darin Dimitrov
If you see /Application/1 in the URL have you got any idea what is happening there? It's there so that I can see what is happening. It displays nicely as well. I like my links to display what I am doing.
Brendan Vogt