1.IBLL Layer
Add method declaration
Public partial interface Iuserinfoservice:ibaseservice<userinfo>{//oh, my Baby,oh, my babyiqueryable< Userinfo> Loagpagedata (Model.Param.UserQueryParam userqueryparam); bool Setrole (int userId, list<int> Roleids);}
BLL Layer Method implementation: The original role is now all deleted, re-add the role (stored in the collection)
public bool Setrole (int userId, list<int> roleids) { var user = DbSession.UserInfoDal.GetEntities (U = U.id = = userId). FirstOrDefault (); User. Roleinfo.clear ();//Chop off all. var allroles = DbSession.RoleInfoDal.GetEntities (r = roleids.contains (r.id)); foreach (Var roleinfo in allroles) { user. Roleinfo.add (roleinfo);//Add the latest characters. } Dbsession.savechanges (); return true; }
2. Controller Userinfocontroller
Public ActionResult setrole (int id) { //the user that is currently setting the role int userId = ID; UserInfo user= userinfoservice.getentities (u = u.id = = ID). FirstOrDefault (); Send all the characters to the foreground viewbag.allroles = roleinfoservice.getentities (U = u.delflag = = delflagnormal). ToList (); The user has the associated role sent to the foreground. Viewbag.exitsroles = (from R in user. Roleinfo Select r.id). ToList (); return View (user);
Set the interface in the 3.view layer to select a role for the user
<div> 4. Submit the selected role to the database
Public ActionResult processsetrole (int UId) { //First: Current User ID --uid //second: all the characters that are ticked. ---> List list<int> setroleidlist =new list<int> (); foreach (var key in Request.Form.AllKeys) { if (key. StartsWith ("Ckb_")) { int roleid = Int. Parse (key. Replace ("Ckb_", "")); Setroleidlist.add (Roleid); } } Userinfoservice.setrole (UId, setroleidlist); Return Content ("OK"); }
Permission Design Database Design association tables are automatically generated in the database (Roleinfo,r_userinfo_actioninfo)
Dbsession is the database context, which is the intermediate variable that contacts the database datebase and. NET, with the lowest level of the operational database
Similar to Dbcontext directly can be a table variable
In MVC, it's just a simple process.
Baseservice in Currentdal can only get the type of DAL for the current services
Dbsession Here The theory is to take all the _dal layer table, but here only declared, not assigned, want to take the other _dal here can only be registered in Spring.net, to dbsession Register all _dal or registered in _service only used _dal ,,,,, or assign values directly in the code (better)
MVC sets roles for users main steps