如何使用会话基于主页的下拉选择重定向用户? [英] How to redirect user based on dropdown selection of master page using session?

查看:80
本文介绍了如何使用会话基于主页的下拉选择重定向用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主页上有一个下拉列表,其中包含部门列表.现在,我有几个页面根据部门选择和更改显示记录列表.

因此,当用户从下拉列表中选择新部门时,我会向MVC控制器进行AJAX调用,并将部门ID发送到该控制器,并将其存储在会话中,并在所有页面上使用该会话,因为我的所有页面都是由部门驱动的./p>

实施方式:

@{
    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);
        }

但是现在假设用户位于/Employee/Skills 页面上,并且当用户打开任何新页面并将部门值更改为"Select department"(id为0)时 并且当用户刷新/Employee/Skills时,用户仍然停留在该页面上,而不是重定向到Employee List页面(/Employee/List).

因此,由于我有很多页面基本上是部门价值驱动的,所以当我将Department ID = 0时,我想将用户重定向到 /Employee/List否则,只需刷新用户当前所在的页面并显示基于department id.

的数据

那么写这样的条件来解决页面丢失是个好主意吗?还是有更好的方法来管理这种情况?

更新

我有以下页面:

我有以下菜单项:

1)EmployeeList

2)DepartmentList

3)技能

4)工资

5)休假管理

6)出勤

7)性能

现在,当用户登录时,该用户将被重定向到以下页面,并且将仅看到2个菜单项,因为当前在布局页面上的部门下拉列表中没有选择部门:

http://localhost:2220/Employee/EmployeeList 

1)EmployeeList

2)DepartmentList

现在我们在http://localhost:2220/Employee/EmployeeList页上,因此当前将列出所有部门的所有员工.

当用户将选择合适的部门时,我将对控制器进行AJAX调用以存储部门ID,这将刷新我的页面,因此现在我将在员工列表"中使用部门ID,因此现在我将根据部门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);
        }

现在选择部门后,所有其他菜单项都将可见(例如:技能,薪水等.)

现在假设用户单击技能菜单项,那么该用户将被重定向到http://localhost:2220/EmployeeSkills/Skills url,在此我将再次 具有上面的方法:

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

原因:这就是为什么我使用会话存储要在所有其他页面上使用的部门ID(技能,工资单等)的原因.

解决方案

仅当用户更改下拉列表(在$(#department").change()内)时,才调用位置更改器逻辑,也可以通过刷新来调用不叫. 将逻辑移到更改方法之外,它可能会起作用.

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.

这篇关于如何使用会话基于主页的下拉选择重定向用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆