Reputation: 441
I use MVC5 EntityFramework 6
I have a clinic app with list of users, every user has account role (Doctor, Nurse, Lab)
how to create DropDownList of(Doctors) or (Nurse) or (Lab)
all users and roles are predefined by default (Individual User Accounts)
users are (ApplicationUser) Roles are (IdentityRole) I update tables names as this Video
all users are in the same table Users
all Roles are in Roles
and I combined between them in UserRoles
Table
Upvotes: 0
Views: 456
Reputation: 441
Its solved using the following code
in Controller
string DoctorRoleID = roleManager.FindByName("Doctor").Id;
ViewBag.DoctorID = new SelectList(db.Users.Where(u => u.Roles.Any(r => r.RoleId == DoctorRoleID)).ToList(), "Id", "Email");
In View
<div class="form-group">
<div class="col-md-2">
@Html.LabelFor(model => model.DoctorID, "DOCTOR", htmlAttributes: new { @class = "control-label col-md-2" })
</div>
<div class="col-md-10">
@Html.DropDownList("DoctorID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.EndUserID, "", new { @class = "text-danger" })
</div>
</div>
Upvotes: 1
Reputation: 108985
Assuming the data for your drop down is id & value pairs, then you just need to select a different query – but returning the same type – depending on the data wanted.
Eg.
IEnumerable<KeyValuePair<int, string>> GetNames(NameType nt) {
if (nt == NameType.Nurse) {
return from n in db.Nurses
select new KeyValuePair {
Key = n.Id,
Value = n.Name
};
} else if (nt == NameType.Doctor) {
return from d in db.Doctors
select new KeyValuePair {
Key = d.Id,
Value = d.ProfessionalDisplayName
};
}
[...]
}
You can of course use a custom type (including anonymous: all anonymous types in the same assembly with the same members (name & value) in the same order are the same type); and encode the type of person in the id (eg. to save keeping track separately).
Upvotes: 0