简体   繁体   中英

How to redirect user based on dropdown selection of master page using session?

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).

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.

根据会话值,您可以在不同页面上重定向。

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