【发布时间】: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