开发者

MVC 2 map routing from one controller to another

开发者 https://www.devze.com 2023-01-17 19:01 出处:网络
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.

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 开发者_Python百科"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


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 }
    );
}
0

精彩评论

暂无评论...
验证码 换一张
取 消