【问题标题】:How to redirect user based on dropdown selection of master page using session?如何使用会话根据母版页的下拉选择重定向用户?
【发布时间】:2016-10-05 15:48:47
【问题描述】:

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

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

实施:

@{
    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 页面上,当用户打开任何新页面并将部门值更改为“选择部门”时(id 将为 0) 当用户刷新 /Employee/Skills 时,用户仍然停留在该页面上,而不是重定向到员工列表页面(/Employee/List)。

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

那么,为丢失页面编写这样的条件是一个好主意,还是有更好的方法来管理这个?

更新

我有以下页面:

我有以下菜单项:

1) 员工名单

2) 部门列表

3) 技能

4) 薪水

5) 休假管理

6) 出勤率

7) 性能

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

http://localhost:2220/Employee/EmployeeList 

1) 员工名单

2) 部门列表

现在我们在 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)

原因:这就是我使用 session 来存储部门 ID 以在所有其他页面(技能、工资单等)上使用的原因。

【问题讨论】:

  • 无法理解你试图用这段代码做什么。 ajax 的全部意义在于保持在同一页面上,而您所做的只是在成功回调中进行重定向。不要使用 ajax(在这种情况下毫无意义)。进行正常提交并在 POST 方法中重定向到适当的方法。
  • 我正在尝试在更改部门下拉列表时在会话中存储新部门值。现在我有很多页面(/员工/技能,/工资单/薪水),只有在用户选择部门时才能访问,因为此页面上的记录是部门驱动的。默认情况下,部门下拉列表中有“选择部门”,用户将在 /Employee/List 上,所以现在当用户选择适当的部门时,用户将能够看到这些页面(技能,薪水)在菜单项中
  • 问题是当用户选择任何部门并转到页面说例如:/Employee/Skill 现在用户在新选项卡上打开新页面并再次将部门下拉选择选择为“选择部门”并刷新 /员工/列表页面,然后用户停留在此页面上,但现在会话值在“选择部门”的情况下将为 0,那么用户如何不重定向到 /员工/列表?
  • 您不能更改您的应用程序以在特定页面的 URL 中包含 departmentId 吗?而不是/Employee/Skill,您将拥有/Employee/Skill/{departmentId}?
  • @PawełHemperek 好的,但是当我的部门下拉列表发生变化时,我将如何生成这个 /Employee/Skill/{departmentId}。我使用的是 Angular js,但路由仅由 mvc 处理

标签: c# asp.net-mvc asp.net-mvc-routing session-variables


【解决方案1】:

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

【讨论】:

  • 但我想我必须在 mvc 端处理路由,我是否必须为所有页面编写这样的条件,因为我有很多页面。你能为我提供更好的解决方案吗
  • 没有“mvc 端”。有客户端和服务器端。对于客户端,你有这个。对于服务器端,您可以配置 mvc 路由或创建类似这样的内容:stackoverflow.com/questions/3067908/…
  • 好的,我已经检查过了,但是在那个答案中,如果用户选择任何部门并且用户当前在页面上,我将如何维护可以说 /Payroll/Salary 并且如果用户再次选择“选择部门(意味着否部门)”,那么用户应该被重定向到 /Employee/List 页面,而不管用户当前在哪个页面上。
  • 将值存储在会话中并构建一个逻辑,该逻辑根据当前位置和会话值决定重定向到哪里。
  • 那么我是否正确地使用了 ajax 方法,但是如果您阅读过@stephen 评论,那么根据他的说法,我根本不需要 ajax。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2020-02-09
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 2014-04-19
    • 2015-05-10
    • 2016-08-31
    相关资源
    最近更新 更多