简体   繁体   English

为用户分配角色 Asp.Net MVC

[英]Assign Roles to Users Asp.Net MVC

Created required Model Class for Users, ROles and Role Mapping.为用户、角色和角色映射创建了所需的 Model Class。 But unable to assign Role to users from View.但无法从 View 为用户分配角色。 Need to create Post function to update Role for specific users.需要创建帖子 function 以更新特定用户的角色。 Here is below code.这是下面的代码。

  1. Users Model Class用户 Model Class
public class Users
    {
        [Key]
        public int ID { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
    }
  1. RoleMaster Model Class RoleMaster Model Class
public class RoleMaster
    {
        [Key]
        public int ID { get; set; }
        public string RollName { get; set; }
    }
  1. UserRolesMapping Model Class用户角色映射 Model Class
public class UserRolesMapping
    {
        [Key]
        public int ID { get; set; }
        public int UserID { get; set; }
        public int RoleID { get; set; }

        [ForeignKey("UserID")]
        public virtual Users Users { get; set; }

        [ForeignKey("RoleID")]
        public virtual RoleMaster RoleMaster { get; set; }
    }
  1. Db Class数据库 Class
public class Db :DbContext
    {
        public DbSet<Users> Users { get; set; }

        public DbSet<RoleMaster> RoleMasters { get; set; }

        public DbSet<UserRolesMapping> UserRolesMappings { get; set; }


    }
  1. RegisterRole Action with GET Method to show user and Role使用 GET 方法的 RegisterRole 操作以显示用户和角色
[HttpGet]        
        public ActionResult RegisterRole()
        {
            using (Db db = new Db())
            {
                ViewBag.Name = new SelectList(db.RoleMasters.ToList(), "RollName", "RollName");
                ViewBag.UserName = new SelectList(db.Users.ToList(), "UserName", "UserName");
                return View();
            }


        }
  1. View to Show Username and Role dropdown to select which is working fine and displaying the correct data.查看以显示用户名和角色下拉列表到 select,它工作正常并显示正确的数据。
@model EmpMVC.Models.Users
@{
    ViewBag.Title = "RegisterRole";
}

<h2>RegisterRole</h2>
@using (Html.BeginForm("RegisterRole", "Users", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <div class="form-group">
        <label class="control-label col-md-2">Select UserName</label>
        <div class="col-md-5">
            @Html.DropDownList("UserName")
        </div>
    </div>

    <div class="form-group">
        <label class="control-label col-md-2">Select Role</label>
        <div class="col-md-5">
            @Html.DropDownList("Name")
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-5">
            <input type="submit" class="btn btn-primary" value="Register" />
        </div>
    </div>
}

Unable to write correct code to update correct profile for specific User listed.无法编写正确的代码来更新列出的特定用户的正确配置文件。

Help much appreciated非常感谢帮助

Got it resolved, thanks to my Friend Aniket on this, helped me with the Debugging process, and was able to connect the dots.得到了解决,感谢我的朋友 Aniket,帮助我进行调试过程,并且能够连接点。 Here is the code that works这是有效的代码

[HttpPost]
        public ActionResult RegisterRole(RoleMaster role, User user)
        {
            using (Db db = new Db())
            {
                UserRolesMapping dto = new UserRolesMapping();

                User userDto = db.Users.FirstOrDefault(x => x.UserName == user.UserName);
                dto.UserID = userDto.UserID;

                RoleMaster roleDto = db.RoleMasters.FirstOrDefault(x => x.RollName == role.RollName);
                dto.RoleID = roleDto.RoleID;

                db.UserRolesMappings.Add(dto);
                db.SaveChanges();

            }
            return RedirectToAction("Roles");
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM