0

I have to following many-to-many model structure:

 public class User
    {    
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid UserId { get; set; }

        [Required]
        [MaxLength(50)]
        public string Name{ get; set; }

        [Required]
        [MaxLength(50)]
        public string Email { get; set; }

        
        public virtual ICollection<UserInRole> UserInRoles { get; set; }
   }

 public class Role
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int RoleId { get; set; }
        public string RoleName { get; set; }
                
        public virtual ICollection<UserInRole> UserInRoles { get; set; }
    }


 public class UserInRole
    {
        [Required]        
        public Guid UserId { get; set; }
    
        [Required]       
        public int RoleId { get; set; }
    
    
        public virtual User User { get; set; }
        public virtual Role Role { get; set; }
    }

ApplicationDbContext:

 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            

            // UserInRole: set both fields as primary
            modelBuilder.Entity<UserInRole>().HasKey(table => new{ table.RoleId, table.UserId});


            modelBuilder.Entity<User>()
                .HasMany(c => c.UserInRoles);


            modelBuilder.Entity<Role>()
                .HasMany(c => c.UserInRoles);

The database is successfully created with the right relationships (many to many) but when I create a new Controller & View using scaffolding - the system generates the CRUD pages without the option to select the role for each user. is it normal? do I need to do this part by myself?

2 Answers 2

1

Yes, as far as I remember you should render one-to-many relations in your views manually.

One of the easiest way I know:

<div class="form-group">
            <label asp-for="User" class="control-label"></label>
            <select asp-for="UserId"
                    class="form-control"
                    asp-items="@(new SelectList(ViewBag.Users, "UserId", "Name"))">
            </select>
        </div>
        <div class="form-group">
            <label asp-for="Role"></label>
            <select asp-for="RoleId"
                    class="form-control"
                    asp-items="@(new SelectList(ViewBag.Roles, "RoleId", "RoleName"))">
            </select>
        </div>

In this example, you send UsersList and RolesList to the View by ViewBag as a SelectList and then render them on View. So as far as I know you need to do it manually to render one-to-many or many-to-many relations because the list needs to be converted to the SelectList for the view to know how to work with it.

You can also take a look at here

Sign up to request clarification or add additional context in comments.

Comments

1

To create a select control, at first you will have to create a list of SelectItem from the Role list:


var roleItems=dbcontext.Roles.Select(i=> new SelectListItem {
Value=i.RoleId.ToString(),
Text=i.RoleName
}).ToArray();

You can use a viewbag to keep the list for the view, but it is a better idea to create a ViewModel class like this

public UserViewModel
{
public User User {get; set;}
public SelectListItem[] RoleItems {get; set;} 
} 

After this you can use this list to create a select input. You can use the sample from @Miraziz answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.