0

I'm working on a MVC application where an user can select a specific project based on a DropDownList. I prefer to have this DropDownList on the layout page, and I would like to use it's value in the routing as each Controller will need this value to select the correct data.

I started out with a PartialView but I believe this is not the way I should go down, I think it's better to use Html.Action on a Generic controller to return the Html for this dropdown.

Now i'm confused though on how to use the value in each URL to feed the controllers with the correct project for data selection.

I have two routes configured, the default is still when the user has not selected a project to work on. How would I use the selected value of the DropDownList in each URL? Or should I follow another route?

        routes.MapRoute(
            name: "Project",
            url: "project/{project}/{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
Martin
  • 397
  • 3
  • 16
  • 1
    Whether you use `@Html.Partial` or `@Html.Action()` is probably irrelevant. Your rendering a dropdown to order to make a selection so the only way to construct a url based on the selection is by using javascript (unless you posting the value of the dropdown doing a redirect in the controller method) –  Nov 23 '14 at 22:03
  • I have just created a base controller that is inherited by all my controllers that inserts the correct information into the ViewBag. This way I can have a standard Html.DropDownList in the layout page. For the value I will most likely do a postback. – Martin Nov 24 '14 at 06:10
  • @StephenMuecke This is the same scenario which i have describe here.http://stackoverflow.com/questions/39878533/how-to-redirect-user-based-on-dropdown-selection-of-master-page-using-session – I Love Stackoverflow Oct 12 '16 at 06:41
  • @Learning, That's not what you really describe in your question (and its unclear why you want to use `Session` in your case). I'll have another look at it in 30 min or so, but it would help if you added that question to include a bit more info on what your trying to achieve. –  Oct 12 '16 at 06:45

0 Answers0