ASP.NET MVC+EF +EasyUI framework implementation of Privilege Management Series (

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

  ASP.NET MVC+EF +EasyUI framework implementation authority series

  (opening)

(1):The frame building

 (2):Design of Demo database access layer

(3):The interface oriented programming

(4 ):The business logic layer of the package   

   (5):The Jquery easyUI       (6): EF context instance management 

(7):DBSession package

(8):The DBSession thread only   

   (9):TT touch version of learning

(10):VSS source code management         (11): verification code implementation and the underlying modify     (12): user asynchronous log and T4 template   

   (13):Authority design of     (14): the main framework of       (15): access to database and user model with error         (16): a variety of verification of user registration 

  (17):Details of the processing of registered users function

  Objective: the front two blog I realized the user registration function, Divided into two parts to introduce, Mainly introduces some simple authentication and user name cannot be empty, The password and confirm password must be the same, So we will not tell this detailed, As long as we tell the specific ideas I think everyone can understand, So many today we for user data and delete user information etc., Of course I will put the main code posted for everyone to have a look, Because a lot of things back is very simple, So I also is roughly speaking., We hope to be able to knock myself again code, So what are know, Below we will begin our today's content.

1. Use of filters (users must log)

  (1)When our website development to here, although we have realized user login information, but if I do not log on I can operate the data in the database, as long as I am in the path direct input address access, not after the login information, such as: if I like this visit, as shown in figure he is you can operate, but the user is not logged in, so we need to solve this problem.

   

  (2)Above we have put forward the demand, we will need to perform, so we have what method can solve this problem? When we think of Session, we can judge whether the session is empty, if to the login page is empty words to jump directly, responsible for the work. So when we log in the time we have the assignment to the session, you can go to the login's blog to see, the main code as follows:

    //If the user information in words the user information is saved to the session

if (UserName != null)

{

Session["UserInfo"] = userInfo;

}

  (3)Then we in the login control which has written Session, Then the controller all pages we login after the inside is to determine whether the user is empty., So is it right? It is redundant? Of course, So we is it right? Put some public things onto the base class (public) go, When we first thought of the global filter Filter, All controllers must pass through the global filter before executing it, But there is a drawback is the login page will also enter the global filter, So here the use of global filter is not too good, The user is not logged in to verify whether the login.

  (4)With the global filter is not easy, so we still have what method? We found we can inherit from Controller, so we think we is it right? Need to a base class inherits from Controller, and let the other control class inherits from a base class we build, so we built a base BaseController class, then let the page need to detect user login inherits from the base class, the following code:

 1 namespace LYZJ.UserLimitMVC.UI.Portal.Controllers
 2 
 3 {
 4 
 5     /// <summary>
 6 
 7     /// The base class BaseController, filter
 8 
 9     /// </summary>
10 
11     public class BaseController : Controller
12 
13     {
14 
15         //The currently logged in user attributes
16 
17         public BaseUser CurrentUserInfo { get; set; }
18 
19        
20 
21         //To base class in the Action to perform a thing before, in a method of Action prior to execution
22 
23         protected override void OnActionExecuting(ActionExecutingContext filterContext)
24 
25         {
26 
27             base.OnActionExecuting(filterContext);
28 
29             //User login information
30 
31             CurrentUserInfo = Session["UserInfo"] as BaseUser;
32 
33             //To determine whether the user is empty
34 
35             if (CurrentUserInfo == null)
36 
37             {
38 
39                 Response.Redirect("/Login/Index");
40 
41             }
42 
43         }
44 
45     }
46 
47 }

  (5)Finally let us control the need to detect user logs are inherited from the base class which can, in this way we can solve this demand, inheritance of code as follows:

public class UserInfoController : BaseController

More than 2 data deletion (the user can select a plurality of data deletion)

  (1)The database operation is crud, then we begin to remove the data now, the deletion of data we are primarily divided into two kinds, one is directly will be removed from the data, is a pseudo delete, modify and delete users only state for deletion, directly will be removed from the data is divided into two a state, a state of a piece of data deletion, one is directly choose a lot of data and then delete delete, because a very simple, here I will not do, I sat directly delete multiple data, so I began to realize direct delete and pseudo delete function.

More than 3 data directly to delete and pseudo delete

  (1)First we realize multi data directly to delete, so we first is to direct the delete button, EasyUI provides the method how to display icon, you need to look at the EasyUI, here is not to say, effect diagram as follows:

   

  (2)Next we need to delete and pseudo add delete asynchronous method to realize this function, we first thought is directly deleted and pseudo deleted when the asynchronous request there is not the same? Of course, is a method for transmitting asynchronous request processing is not the same, other places are the same in front, so we remember a word code public time must package method, so here we also package method, and then give an indicator whether we pseudo delete or delete directly, so the foreground the main asynchronous request code.:

  1        //Realizing the direct delete data and the pseudo data deletion method
  2 
  3         function deleteBaseUserInfo(not) {
  4 
  5             //User selected data ID
  6 
  7             var rows = $("#test").datagrid("getSelections");
  8 
  9             //First determine whether the user has chosen to delete the data, then the loop will user selected data transferred to background
 10 
 11             if (rows.length >= 1) {
 12 
 13                 //Traverse the user selected data information, this is the user the option to delete the user ID information
 14 
 15                 var ids = "";   //1,2,3,4,5
 16 
 17                 for (var i = 0; i <rows.length; i++) {
 18 
 19                     //Asynchronous sends the deletion of the ID to the background, the background removed, return, the washing table
 20 
 21                     ids += rows[i].ID + ",";
 22 
 23                 }
 24 
 25                 //Finally removed the last,
 26 
 27                 ids = ids.substring(0, ids.length - 1);
 28 
 29                 //Gets the user selected user information, if the user chooses to have logged on user would need to give the information cannot be deleted
 30 
 31                 var userNames = "";
 32 
 33                 for (var i = 0; i <rows.length; i++) {
 34 
 35                     userNames += rows[i].UserName + ",";
 36 
 37                 }
 38 
 39                 userNames = userNames.substring(0, userNames.length - 1);
 40 
 41                 //First take out what is directly deleted or pseudo delete the asynchronous request address and whether the parameters of pseudo deleted
 42 
 43                 var postData = "";
 44 
 45                 if (not == null) {
 46 
 47                     postData = {
 48 
 49                         ID: ids, UserName: userNames
 50 
 51                     }
 52 
 53                 }
 54 
 55                 else {
 56 
 57                     postData = {
 58 
 59                         ID: ids, UserName: userNames, Not: not
 60 
 61                     }
 62 
 63                 }
 64 
 65                 //And then confirm the asynchronous request information to the background delete data
 66 
 67                 $.messager.confirm("Delete information", "You to confirm the deletion<font color='red' size='3'>" + userNames + "</font>The user you? ", function (DeleteUser) {
 68 
 69                     if (DeleteUser) {
 70 
 71                         $.get("/UserInfo/DeleteUsers", postData, function (data) {
 72 
 73                             if (data == "OK") {
 74 
 75                                 //Friendship prompt the user deleted successfully, refresh tables
 76 
 77                                 $.messager.alert("Tips", "Successfully deleted");
 78 
 79                                 $("#test").datagrid("reload");
 80 
 81                                 //When the delete is completed, the second time delete remember the last information, this is not possible, so we need to clear the first information
 82 
 83                                 //The first method
 84 
 85                                 rows.length = "";
 86 
 87                                 //Second kinds of methods
 88 
 89                                 $("#test").datagrid("clearSelections");
 90 
 91                             }
 92 
 93                             else {
 94 
 95                                 $.messager.alert("Tips", "Delete failed:" + data);
 96 
 97                             }
 98 
 99                         });
100 
101                     }
102 
103                 });
104 
105             }
106 
107             else {
108 
109                 alert("Please select the one you want to delete the data");
110 
111             }
112 
113         };

  (3)Here we need to pay attention to is that if we present the user is logged in the system, if we want to delete this data, because the theory is not deleted, so in the code above I also do some operation name also transmitted to taiwan. So we can be in the background to determine the login username is used again, if use is not allowed to delete, responsible for allowing the logged on user delete information.

  (4)From the above code can detailed I do see a lot of comments, if you do not know how to look, here is not described in detail, when we send out the asynchronous request, this time we want to achieve our request sent by the controller, the main controller is implemented in the code so we as follows, I also write a lot of comments, here is not to explain in detail. The following code:

 1    //Delete user information
 2 
 3         public ActionResult DeleteUsers(BaseUser userInfo, string ID, string UserName, string Not)
 4 
 5         {
 6 
 7             //The first judgment is the user logs into, if the system is being used for this user, do not allow users to delete
 8 
 9             userInfo = Session["UserInfo"] as BaseUser;
10 
11             var userName = userInfo.UserName; //User login information
12 
13             var uIDsName = UserName.Split(',');  //Will pass over the username is divided into display a
14 
15             List<string> listUserInfo = new List<string>();
16 
17             foreach (var Name in uIDsName)
18 
19             {
20 
21                 listUserInfo.Add(Name);
22 
23             }
24 
25             if (listUserInfo.Contains(userName))
26 
27             {
28 
29                 return Content("Containing the user is using, no delete");
30 
31             }
32 
33             //Let us start to delete user information
34 
35             //The first judgment to confirm whether from the transmitted information
36 
37             if (string.IsNullOrEmpty(ID))
38 
39             {
40 
41                 return Content("Please choose to delete data");
42 
43             }
44 
45             var idStrs = ID.Split(',');  //String intercept passed
46 
47             List<int> deleteIDList = new List<int>();
48 
49             foreach (var idStr in idStrs)
50 
51             {
52 
53                 deleteIDList.Add(int.Parse(idStr));
54 
55             }
56 
57             if (Not != null)
58 
59             {
60 
61                 //Pseudo delete, is based on the user's ID to modify the information, the first query entity information
62 
63                 foreach (var deleteId in deleteIDList)
64 
65                 {
66 
67                     var EditUserDeleteIsNot = _userInfoService.LoadEntities(c => c.ID == deleteId).FirstOrDefault();
68 
69                     EditUserDeleteIsNot.DeletionStateCode = 1;
70 
71                     _userInfoService.UpdateEntity(userInfo);
72 
73                 }
74 
75                 return Content("OK");
76 
77             }
78 
79             else
80 
81             {
82 
83                 //The last execution data bulk deletion methods
84 
85                 if (_userInfoService.DeleteUsers(deleteIDList) > 0)
86 
87                 {
88 
89                     return Content("OK");
90 
91                 }
92 
93             }
94 
95             return Content("Delete failed, please check");
96 
97         }
98 
99     }

  (5)Then according to the code above we can see that we realize the pseudo delete and delete, but I also will batch delete code again for the package, on the business logic layer, so the main code business logic layer.:

 1    //Realization of batch delete data
 2 
 3         public int DeleteUsers(List<int> deleteIds)
 4 
 5         {
 6 
 7             foreach (var deleteID in deleteIds)
 8 
 9             {
10 
11                 _DbSession.BaseUserRepository.DeleteEntity(new BaseUser()
12 
13                 {
14 
15                     ID = deleteID
16 
17                 });
18 
19             }
20 
21             return _DbSession.SaveChanges();
22 
23         }

4 Summary

  (1)This blog we roughly it says so much, we first introduced the filter, and then introduces the various users delete information in detail, if you are interested can go to see my code, the main part of the code or to share with everyone, I this is not described in detail.,

  (2)Below I show the function of drawing this blog, the image is as follows:

   

   

  .NET developer blog: www.kencery.com

    Kencery returns the series begins

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Stev at November 19, 2013 - 8:18 AM