• 主攻ASP.NET.3.5.MVC3.0架构之重生:CheckBox(十一)


    效果图

    UserRoleViewModel.cs

     1     public class UserRoleViewModel
     2     {
     3         RoleRepository rolerepository = new RoleRepository();
     4         //BMK 角色列表
     5         public IQueryable<Role> rolelist { getset; }
     6         public UserRoleViewModel()
     7         {
     8             rolelist = rolerepository.GetModelList();
     9         }
    10     }

     UserController.cs

     //BMK 用户添加提交数据
            [Authorize, AcceptVerbs(HttpVerbs.Post)]
            public ActionResult Add(User model,string[] roleid, FormCollection fc)
            {
                model.State = 1;
                model.CreateTime = DateTime.Now;
                model.PassWord = StringHelper.GetMD5(Request.Form["PassWord"]);
                userrepository.SaveOrEditModel(model);
                //添加用户角色类型
                for (int i = 0; i <roleid.Length; i++)
                {
                    UserRoleMapping URM = new UserRoleMapping();
                    URM.UserID = model.UserID;
                    URM.RoleID = int.Parse(roleid[i]);
                    urmrepository.SaveOrEditModel(URM);
                }
                return RedirectToAction("Index");
            }
     public IQueryable<UserRoleMapping> urmlist { getset; }
            //BMK 用户删除
            [Authorize]
            public ActionResult Delete(int id)
            {
                var model = userrepository.GetModel(id);

                if (model != null)
                {
                    //删除所选用户全部角色关系
                    urmlist = urmrepository.GetModelListByUserID(id);
                    if (urmlist.Count() > 0)
                    {
                        foreach (var item in urmlist)
                        {
                            urmrepository.DeleteModel(item.UserRoleMappingID);
                        }
                        userrepository.DeleteModel(id);
                    }
                    return RedirectToAction("Index");
                }
                else
                    return View("404");
            }

    页面代码

    <b>&nbsp;&nbsp;&nbsp;色:&nbsp;&nbsp;&nbsp;&nbsp;</b>
                            <% foreach (var item in Model.rolelist)
                               { 
    %>
                            <input id="roleid" name="roleid" type="checkbox"
                                value
    ="<%=item.RoleID %>">
                            <span>
                                <%=item.RoleName %></span>
                            <%%>
  • 相关阅读:
    python 中给文件加锁——fcntl模块
    python生成二维码
    uwsgi常用配置
    php curl实现get和post请求
    python __enter__ 与 __exit__的作用,以及与 with 语句的关系
    python文件操作总结
    Python时间,日期,时间戳之间转换
    Python random模块(获取随机数)
    wigs的理解和应用
    shiro中接入单点登录功能
  • 原文地址:https://www.cnblogs.com/cube/p/2779168.html
Copyright © 2020-2023  润新知