MVC sets roles for users main steps

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.