0

I have the following page structure in asp.net mvc 4.

 Department            Full Time Employees      Part Time Employees
-----------            ---------------------------------------------
Department 1          |1. Employee 1                  
Department 2          |2. Employee 2
                      |

Where Department 1, Department 2, Full Time Employees and Part Time Employees are links. Clicking on any link results in displaying some employees. Here are the possible options

  1. When the user browse the page for the first time, full time employees in first department (Department 1) are displayed.
  2. When the user clicks Department 2, full time employees from department 2 are displayed (since the full time link is already selected by default)
  3. When the user clicks Part Time, part time employees are displayed for the selected department.

I don't know how to construct the action links for these different links. The target result that I am aiming at is something like

  1. www.example.com/Department1/fulltime
  2. www.example.com/Department1/parttime
  3. www.example.com/Department2/fulltime
  4. www.example.com/Department2/parttime

    Thanks

Update

The issue here is how to construct the action link not how to route them to the controller. Currently I do have an action is my Home Controller which accepts the Department and full time/part time. What I am not able to figure out is how to keep track of things. For example If user click the Part Time link while Department 2 is already selected, how can the link for Part Time convey the selected Department to the action method so that the controller knows it has to display part time employees from selected department. Similarly with Part Time selected, if user clicks Department, how does the link for department convey the information to controller that it has to display part time employees in the clicked department.

ZedBee
  • 2,320
  • 7
  • 39
  • 61
  • Check [custom routing](http://www.codeproject.com/Articles/299531/Custom-routes-for-MVC-Application) – Artless Apr 01 '13 at 09:45

1 Answers1

0

As far as I understand I think what you need is routing. Have a look at

MVC.Net Routing

and the accepted answer. There is a brief explanation how to create custom routes to achieve what you describe above. You will need to create a controller with an action with two arguments, string department and another one (a string or enum or anything the DefaultModelBinder can bind) for full- or part time and then map your route to this controller and action.

Since routing works bidirectional, mapping your routes correctly is mandatory to create proper actionlinks. As you can see in the msdn, the System.Web.Routing.Route class has also a virtual method GetVirtualPath. As soon as you try to create an actionlink using the Html.ActionLink-method (or one of its overloads), the framework will "query" the routes (honoring route constraints, etc.) and build the so-called virtual path based on the route values provided.

Assuming that you created a controller called HomeController which looks similar to

public sealed class HomeController : Controller
{
    public ActionResult Index(string department, EmployeeType employeeType)
    {
        /* ... */
    }
}

public enum EmployeeType
{
    FullTime,
    PartTime,
}

you could create an actionlink (from within any view) as follows:

Html.ActionLink(
    "Full-time employees of department 1",
    "Index", // action
    "Home", // controller
    new
    {
        department = "Department1", // first argument
        employeeType = EmployeeType.FullTime, // second argument
    },
    htmlAttributes: null);

This then creates a link with an url according to your needs as long as the matching route implements GetVirtualPath correctly (whatever that means). The default implementation may create an url like /Home/Index?department=Department1&employeeType=0. Then you need to derive from System.Web.Routing.Route, override and implement GetVirtualPath and register the route in App_Start or Global.asax.

Community
  • 1
  • 1
Peit
  • 882
  • 6
  • 17