如何使用会话基于主页的下拉选择重定向用户?

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

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

执行:

@{ int departmentId = 0; int.TryParse(Convert.ToString(Session["departmentId"]), out departmentId); }  //filling dropdown based on departmentId  $(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页面上,当用户打开任何新页面并将部门值更改为“选择部门”(id将为0)时,当用户刷新/员工/技能时,仍然用户停留在该页面而不是重定向到“员工列表”页面(/ Employee / List)。

因为我有很多基本上是部门价值驱动的页面所以当我有部门ID = 0时我想将用户重定向到/Employee/List其他只是刷新当前用户所在的页面并根据department id.显示数据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,这将刷新我的页面,所以现在我将在Employee列表中提供部门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()),同时刷新它不会被调用。 将您的逻辑移到更改方法之外,它可能会起作用。

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