简体   繁体   中英

ASP.NET MVC Routing for Master / Detail Views with “Detail Children”

I have the basic Master / Detail Views working great with the default ASP.NET MVC Route; however I would like to build some URLs like this:

/Class/Details/5 -- General Detail view [Working]

What I'm not sure about (and I'm not tied to this URL format, just something roughly equalivent.)

/Class/5/Details/Logs -- Detail View with Logs
/Class/5/Details/Status -- Detail View with current Status

Another way to put this, is like this:

/{controller}/{id}/{controllerSpecificMaster}/{action}/

What I'm trying to avoid, is cluttering up my Views\\Class directory with a bunch of Views, which are all basically derivatives of the Details view.

I'm on ASP.NET MVC 1 and .NET 3.5 SP1.

The first thing you need to get down are your routes. You may have already done this, but in case you haven't, here's a route entry that will handle your custom route needs:

routes.MapRoute("Master_Detail",
                "{controller}/{id}/{controllerSpecificMaster}/{action}",
                new { controller = "Class", 
                      action = "Index", 
                      id = UrlParameter.Optional,
                      controllerSpecificMaster = "Details"
                });

Then, in your action methods where you want to use the route-specified master page, just include the route key in your method arguments, and then pass it to the view:

public ActionResult Logs(int id, string controllerSpecificMaster)
{
    //do something

    //return view with master name as argument
    return View("Logs", controllerSpecificMaster);
}

If you have to do this a lot, I would suggest creating a custom view engine and override the FindView() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM