c# - How to construct Actionlinks -
i have 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 , part time employees links. clicking on link results in displaying employees. here possible options
- when user browse page first time, full time employees in first department (department 1) displayed.
- when user clicks department 2, full time employees department 2 displayed (since full time link selected default)
- when user clicks part time, part time employees displayed selected department.
i don't know how construct action links these different links. target result aiming @ like
- www.example.com/department1/fulltime
- www.example.com/department1/parttime
- www.example.com/department2/fulltime
www.example.com/department2/parttime
thanks
update
the issue here how construct action link not how route them controller. have action home controller accepts department , full time/part time. not able figure out how keep track of things. example if user click part time link while department 2 selected, how can link part time convey selected department action method controller knows has display part time employees selected department. part time selected, if user clicks department, how link department convey information controller has display part time employees in clicked department.
as far understand think need routing. have at
and accepted answer. there brief explanation how create custom routes achieve describe above. need create controller action 2 arguments, string department
, 1 (a string or enum or defaultmodelbinder
can bind) full- or part time , map route controller , action.
since routing works bidirectional, mapping routes correctly mandatory create proper actionlinks. can see in msdn, system.web.routing.route
class has virtual method getvirtualpath
. try create actionlink using html.actionlink
-method (or 1 of overloads), framework "query" routes (honoring route constraints, etc.) , build so-called virtual path based on route values provided.
assuming created controller called homecontroller
looks similar to
public sealed class homecontroller : controller { public actionresult index(string department, employeetype employeetype) { /* ... */ } } public enum employeetype { fulltime, parttime, }
you create actionlink (from within view) 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 creates link url according needs as long matching route implements getvirtualpath
correctly (whatever means). default implementation may create url /home/index?department=department1&employeetype=0
. need derive system.web.routing.route
, override , implement getvirtualpath
, register route in app_start
or global.asax
.
Comments
Post a Comment