2ge ListBox 之間的 上下選擇,MVC ViewModel,2geviewmodel
HtmlCode
1 public RoleViewModel generateActionList(string roleId, List<string> assignidlist)
2 {
3 if (string.IsNullOrEmpty(roleId) && assignidlist == null)
4 {
5 List<IcActions> availableList = this.ServiceLocator.GetService<IRoleActionService>().GetAllActions(appid);
6 List<IcActions> assignList = new List<IcActions>();
7 PortalProj.Common.Entities.IcRoles icRoles = new PortalProj.Common.Entities.IcRoles();
8 return RoleViewModel.GetFromIcRoles(icRoles, assignList, availableList);
9 }
10 else if (string.IsNullOrEmpty(roleId) && assignidlist != null)
11 {
12 List<IcActions> availableList = this.ServiceLocator.GetService<IRoleActionService>().GetAllActions(appid);
13 List<IcActions> assignList = this.ServiceLocator.GetService<IRoleActionService>().GetActionById(assignidlist).OrderBy(m => m.ActionSeq).ToList();
14 PortalProj.Common.Entities.IcRoles icRoles = new PortalProj.Common.Entities.IcRoles();
15 foreach (var item in assignList)
16 {
17 var itemToRemove = availableList.Single(i => i.ActionId == item.ActionId);
18 if (itemToRemove != null)
19 availableList.Remove(itemToRemove);
20 }
21 return RoleViewModel.GetFromIcRoles(icRoles, assignList, availableList);
22 }
23 else
24 {
25 PortalProj.Common.Entities.IcRoles icRoles = this.ServiceLocator.GetService<IIcRolesService>().GetRoleById(roleId);
26 List<IcActions> availableList = this.ServiceLocator.GetService<IRoleActionService>().GetAllActions(appid);
27 List<IcActions> assignList = this.ServiceLocator.GetService<IRoleActionService>().GetActionById(assignidlist).OrderBy(m => m.ActionSeq).ToList();
28 foreach (var item in assignList)
29 {
30 var itemToRemove = availableList.Single(i => i.ActionId == item.ActionId);
31 if (itemToRemove != null)
32 availableList.Remove(itemToRemove);
33 }
34 return RoleViewModel.GetFromIcRoles(icRoles, assignList, availableList);
35 }
36 }
Function Code
1 public class RoleViewModel
2 {
3 public string Id { get; set; }
4
5 [Required(ErrorMessage = "'Role' cannot be empty.")]
6 public string Name { get; set; }
7
8 [Required(ErrorMessage = "'Selected Functions' cannot be empty.")]
9 public IList<ActionViewModel> AssignedActions { get; set; }
10 public IList<ActionViewModel> AvailableActions { get; set; }
11 public string[] AssignedSelected { get; set; }
12 public string[] AvailableSelected { get; set; }
13
14 public static RoleViewModel GetFromIcRoles(PortalProj.Common.Entities.IcRoles item, List<IcActions> assignedList, List<IcActions> availableList)
15 {
16 var role = new RoleViewModel
17 {
18 Id = item.RoleId,
19 Name = item.RoleName
20 };
21
22 role.AssignedActions = new List<ActionViewModel>();
23 role.AvailableActions = new List<ActionViewModel>();
24
25 if (assignedList != null)
26 {
27 foreach (var assigned in assignedList)
28 {
29 var a = new ActionViewModel
30 {
31 Id = assigned.ActionId,
32 Code = assigned.ActionCode,
33 Name =Convert.ToString(assigned.ActionSeq)+ "." + assigned.ActionDesc
34 };
35
36 role.AssignedActions.Add(a);
37 }
38 }
39
40 if (availableList != null)
41 {
42 foreach (var available in availableList)
43 {
44 var a = new ActionViewModel
45 {
46 Id = available.ActionId,
47 Code = available.ActionCode,
48 Name = Convert.ToString(available.ActionSeq) + "." + available.ActionDesc
49 };
50
51 role.AvailableActions.Add(a);
52 }
53 }
54
55 return role;
56 }
57
58 public RoleViewModel()
59 {
60 AssignedActions = new List<ActionViewModel>();
61 }
62 public void AddAction(ActionViewModel item)
63 {
64 AssignedActions.Add(item);
65 }
66 }
ViewModel Code