3

I have a dropdown on my master page which contains departments list.Now i have several pages which displays list of records based on department selection and change.

So when user select new department from dropdown I make an AJAX call to my MVC controller and send department id to that controller and store it in session and used that session across all the pages as because all my pages are department driven.

Implementation:

@{
    int departmentId = 0;
    int.TryParse(Convert.ToString(Session["departmentId"]), out departmentId);
}
 <select id="department" name="department" class="form-control"> //filling dropdown based on departmentId
</select>

 $(document).ready(function () {
            $("#department").change(function () {
                var departmentId = $(this).val();
                $.ajax({
                    url: "@Url.Action("ChangeDepartment", "Home")",
                    data: { id: departmentId },
                    success: function (e) {
                  if ('@departmentId' > 0) {
                          if (window.location.pathname == '/Employee/Skill') {
                         window.location.href = '/Employee/Skill';
                         } else if (window.location.pathname == '/Payroll/Salary') {
                              window.location.href = '/Payroll/Salary';
                } else {
                    window.location = window.location;
                }
            }
            else {
                window.location.href = '/Employee/List';
            }
                }
                });
            });
        });
public JsonResult ChangeDepartment(int id)
        {
            Session["departmentId"] = id;
            return Json(id, JsonRequestBehavior.AllowGet);
        }

But now suppose user is on /Employee/Skills page and when user open any new page and change department value to "Select department"(id will be 0) and when user refresh /Employee/Skills then still user stays on that page instead of redirecting to Employee List page(/Employee/List).

So as I have lots of pages which are basically department value driven so when I will have Department id= 0 I would like to redirect user to /Employee/List else just refresh the page on which user is currently there and show data based on department id.

So is this a good idea to write conditions like this for lost of pages or is there any better way to manage this?

Update

I have following pages:

I have following menu items:

1) EmployeeList

2) DepartmentList

3) Skills

4) Salary

5) LeaveManagement

6) Attendance

7) Performance

Now when user will login then user would be redirected to below pages and will see only 2 menu items as because currently there will be no department selected in department dropdown which is on layout page:

http://localhost:2220/Employee/EmployeeList 

1) EmployeeList

2) DepartmentList

Now as we are on http://localhost:2220/Employee/EmployeeList page so this will currently list all employees of all department.

When user will select appropriate department then I will make an AJAX call to controller to store department id and that will refresh my page so now I would have department id available in Employee list so now I will get Employee list based on department id:

[HttpGet]
        public JsonResult getEmployees()
        {
          int departmentId = 0;
          int.TryParse(Convert.ToString(Session["departmentId"]), out departmentId);
          if(departmentId==0)
               EmployeeRepository.GetEmployee(0);
           else
               EmployeeRepository.GetEmployee(departmentId);
        }

Now after selecting department all other menu items will be visible (for eg:Skills, Salary etc..)

Now suppose user clicks on skills menu item then user will be redirected to http://localhost:2220/EmployeeSkills/Skills url where again I will have method like above:

[HttpGet]
        public JsonResult getSkills()
        {
          int departmentId = 0;
          int.TryParse(Convert.ToString(Session["departmentId"]), out departmentId);
          SkillRepository.GetSkills(departmentId);//sometimes i am getting error here because of Session["departmentId" becomes null
        }

http://localhost:2220/Employee/EmployeeList 
http://localhost:2220/Department/DepartmentList

    http://localhost:2220/EmployeeSkills/Skills  (Will not be accessible and visible without department selection)
    http://localhost:2220/Payroll/Salary  (Will not be accessible and visible without department selection)
    http://localhost:2220/EmployeeLeave/LeaveManagement  (Will not be accessible and visible without department selection)
    http://localhost:2220/EmployeeAttendance/Attendance  (Will not be accessible and visible without department selection)
    http://localhost:2220/EmployeePerformance/Performance  (Will not be accessible and visible without department selection)

Reason: That is why I am using session to store department id to use on all other pages (Skills, Payroll leaves etc).

halfer
  • 19,824
  • 17
  • 99
  • 186
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • Impossible to understand what your trying to do with this code. The whole point of ajax is to stay on the same page, and all your doing is redirecting in the success callback. Do not use ajax (it pointless in this case). Make a normal submit and in the POST method redirect to the appropriate method. –  Oct 06 '16 at 23:30
  • I am trying to store new department value in session on change of department dropdown.now i have lots of pages(/Employee/Skill,/Payroll/Salary) which are accessible only when user selects department because records on this pages are department driven.by default there will be "Select Department" in department dropdown and user will be on /Employee/List so now when user selects appropriate department then user would be able to see those page(Skill,Salary) in menu items – I Love Stackoverflow Oct 07 '16 at 05:17
  • Problem is when user selects any department and goes to page say for eg:/Employee/Skill and now user opens new page on new tab and again make department dropdown selection to "Select department" and refreshes /Employee/List page then user stays on this page but now session value would be 0 in case of "select department" then how user doesnt redirect to /Employee/List? – I Love Stackoverflow Oct 07 '16 at 05:20
  • 1
    Couldn't you change your application to include `departmentId` in URL to specific pages? Instead of `/Employee/Skill` you would have `/Employee/Skill/{departmentId}`? – Paweł Hemperek Oct 10 '16 at 10:59
  • @PawełHemperek Ok but when my dropdown of department will change then how i will generate this /Employee/Skill/{departmentId}.I am using angular js but routing is handle by mvc only – I Love Stackoverflow Oct 10 '16 at 13:49
  • @Learning Okay, that doesn't make any sense. Why would you use Angular which is an MVC framework, but handle routing in ASP.NET? If you want to implement this right, you should consider building some REST API and use Angular to communicate with this API. – Paweł Hemperek Oct 10 '16 at 13:51
  • @PawełHemperek My application is not an SPA application.i am just using angular js for client side – I Love Stackoverflow Oct 10 '16 at 14:01
  • Is both the menu and the dropdown on the layout page? Is the idea that if the user selects `Department 1`, and then clicks the say `LeaveManagement` menu item, it will redirect to a view that only display `LeaveManagement` associated with `Department 1`,but if the users selects no department, then it will redirect to the same view but display `LeaveManagement` for all departments? –  Oct 12 '16 at 07:26
  • @StephenMuecke:Yes Menu items and department are both on the layout page.Yes the idea is if user select Department1 then other menu items(Skills,Leavemanagement,Payroll etc will be displayed) in menu items and then if user click on LeaveManagement then user will be redirected to Leave management on following url( http://localhost:2220/EmployeeLeave/LeaveManagement) and will display leave records associated with department 1 but now if make user make No department selection from dropdown then user will be redirected to http://localhost:2220/Employee/EmployeeList – I Love Stackoverflow Oct 12 '16 at 07:30
  • and then Skills,Payroll etc will gets hidden from menu items because without appropriate department selection i will not allow user to access other pages(Skills,Payroll etc..) – I Love Stackoverflow Oct 12 '16 at 07:32
  • OK, Session is not the right approach and you can just use javascript to update the `href` attributes of the links based on the selected department, so that if you select a department with say `ID=4` then the `href` will redirect to `.../EmployeeManagement/4`. –  Oct 12 '16 at 07:35
  • @StephenMuecke Actually i have think of generating department id in query string but then user can change department id in browser url because what will happen that suppose i have department 100 and 101 and user of department 100 is login and currently viewing leave management records of deparment 100 but now if user changes query string with 101 then still he would be able to see leave management records of department 101 that is why i change my mind to session based approach. – I Love Stackoverflow Oct 12 '16 at 07:45
  • Sorry, I don't get it. If the dropdown contains options for departments 100 and 101, then they can select it anyway. Are you needing to limit the departments a user can select? –  Oct 12 '16 at 08:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125473/discussion-between-learning-and-stephen-muecke). – I Love Stackoverflow Oct 12 '16 at 08:03

2 Answers2

2

Your location changer logic is called only when the user changes the dropdown (within $("#department").change() ), also by refresh it is not called. Move your logic outside of the change method and it might work.

Dexion
  • 1,101
  • 8
  • 14
  • But i guess i have to handle routing on mvc side and do i have to write conditions like this for all the pages as i am having lots of pages.Can you provide me better solution for this please – I Love Stackoverflow Oct 11 '16 at 08:26
  • there is no "mvc side". There are client side and server side. for client side, you have this. for server side, you can configure mvc routing or create something like this: http://stackoverflow.com/questions/3067908/session-is-null-in-iroutehandler-gethttphandler-with-asp-net-routing – Dexion Oct 11 '16 at 09:06
  • Ok i have checked that but in that answer how i will maintain that if user selects any department and user is currently on page lets say /Payroll/Salary and if again user select "select department(means no department)" then user should be redirected to /Employee/List page irrespective on which page user currently is. – I Love Stackoverflow Oct 11 '16 at 09:21
  • 1
    store the value in the session and build a logic which decides where to redirect based on the current location and the session value. – Dexion Oct 11 '16 at 11:14
  • So am I doing that correct that ajax approach but if you have read @stephen comment then according to him I dont need ajax at all. – I Love Stackoverflow Oct 11 '16 at 13:43
  • yes, you don't need ajax. you need a normal javascript or postback or other server side code – Dexion Oct 12 '16 at 05:56
0

Based on the session values you could redirect on different pages.