我有一个名为access
linked with FK to user's table
的表格,因此,用户拥有一组访问权限。
这样,我的控制器返回一个包含所有访问的列表。
我想创建一个包含在用户桌面上的所有用户的下拉列表。
为此,我按照this问题的第一个回答。
我会在这里粘贴我的控制器的代码,它返回一个视图做索引:
public ActionResult Index(string searchString, string sortOrder)
{
ViewBag.IDSortParm = sortOrder == "ID_asc" ? "ID_desc" : "ID_asc";
ViewBag.EmailSortParm = sortOrder == "Email_asc" ? "Email_desc" : "Email_asc";
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
ViewBag.NameSortParm = sortOrder == "Name_asc" ? "Name_desc" : "Name_asc";
var userAccesses = db.UserAccesses.Include(u => u.User);
//UserNamesToList = GetUserNamesList();
/*
var NamesToListQ = userAccesses.Select(x => new SelectListItem
{
Value = x.User.Id,
Text = x.User.Name
});
var NamesToList = new SelectList(NamesToListQ, "Value", "Text");
*/
if (User.IsInRole("Admin"))
{
userAccesses = db.UserAccesses.Include(u => u.User);
switch (sortOrder)
{
case "Date":
userAccesses = db.UserAccesses.Include(u => u.User).OrderBy(s => s.Access);
break;
case "date_desc":
userAccesses = db.UserAccesses.Include(u => u.User).OrderByDescending(s => s.Access);
break;
case "Email_desc":
userAccesses = db.UserAccesses.Include(u => u.User).OrderByDescending(s => s.User.Email);
break;
case "Email_asc":
userAccesses = db.UserAccesses.Include(u => u.User).OrderBy(s => s.User.Email);
break;
case "ID_desc":
userAccesses = db.UserAccesses.Include(u => u.User).OrderByDescending(s => s.UserAcessID);
break;
case "ID_asc":
userAccesses = db.UserAccesses.Include(u => u.User).OrderBy(s => s.UserAcessID);
break;
case "Name_asc":
userAccesses = db.UserAccesses.Include(u => u.User).OrderBy(s => s.User.Name);
break;
case "Name_desc":
userAccesses = db.UserAccesses.Include(u => u.User).OrderBy(s => s.User.Name);
break;
default:
userAccesses = db.UserAccesses.Include(u => u.User).OrderBy(s => s.UserAcessID);
break;
}
if (!String.IsNullOrEmpty(searchString))
{
userAccesses = db.UserAccesses.Include(u => u.User)
.Where(s => s.User.Email.Contains(searchString) || s.User.Id.Contains(searchString));
switch (sortOrder)
{
case "Date":
userAccesses = db.UserAccesses.Include(u => u.User)
.Where(s => s.User.Email.Contains(searchString) || s.User.Id.Contains(searchString))
.OrderBy(s => s.Access);
break;
case "date_desc":
userAccesses = db.UserAccesses.Include(u => u.User)
.Where(s => s.User.Email.Contains(searchString) || s.User.Id.Contains(searchString))
.OrderByDescending(s => s.Access);
break;
case "Email_desc":
userAccesses = db.UserAccesses.Include(u => u.User)
.Where(s => s.User.Email.Contains(searchString) || s.User.Id.Contains(searchString))
.OrderByDescending(s => s.User.Email);
break;
case "Email_asc":
userAccesses = db.UserAccesses.Include(u => u.User)
.Where(s => s.User.Email.Contains(searchString) || s.User.Id.Contains(searchString))
.OrderBy(s => s.User.Email);
break;
case "ID_desc":
userAccesses = db.UserAccesses.Include(u => u.User)
.Where(s => s.User.Email.Contains(searchString) || s.User.Id.Contains(searchString))
.OrderByDescending(s => s.UserAcessID);
break;
case "ID_asc":
userAccesses = db.UserAccesses.Include(u => u.User)
.Where(s => s.User.Email.Contains(searchString) || s.User.Id.Contains(searchString))
.OrderBy(s => s.UserAcessID);
break;
case "Name_asc":
userAccesses = db.UserAccesses.Include(u => u.User)
.Where(s => s.User.Email.Contains(searchString) || s.User.Id.Contains(searchString))
.OrderBy(s => s.User.Name);
break;
case "Name_desc":
userAccesses = db.UserAccesses.Include(u => u.User)
.Where(s => s.User.Email.Contains(searchString) || s.User.Id.Contains(searchString))
.OrderByDescending(s => s.User.Name);
break;
default:
userAccesses = db.UserAccesses.Include(u => u.User)
.Where(s => s.User.Email.Contains(searchString) || s.User.Id.Contains(searchString))
.OrderBy(s => s.Access);
break;
}
}
}
else
{
var id = User.Identity.GetUserId();
userAccesses = db.UserAccesses.Where(u => u.UserID == id).Include(i => i.User).Where(u => u.UserID == id);
switch (sortOrder)
{
case "Date":
userAccesses = db.UserAccesses.Where(u => u.UserID == id)
.Include(i => i.User)
.Where(u => u.UserID == id)
.OrderBy(s => s.Access);
break;
case "date_desc":
userAccesses = db.UserAccesses.Where(u => u.UserID == id)
.Include(i => i.User)
.Where(u => u.UserID == id)
.OrderByDescending(s => s.Access);
break;
case "Email_desc":
userAccesses = db.UserAccesses.Where(u => u.UserID == id)
.Include(i => i.User)
.Where(u => u.UserID == id)
.OrderByDescending(s => s.User.Email);
break;
case "Email_asc":
userAccesses = db.UserAccesses.Where(u => u.UserID == id)
.Include(i => i.User)
.Where(u => u.UserID == id)
.OrderBy(s => s.User.Email);
break;
case "ID_desc":
userAccesses = db.UserAccesses.Where(u => u.UserID == id)
.Include(i => i.User)
.Where(u => u.UserID == id)
.OrderByDescending(s => s.UserAcessID);
break;
case "ID_asc":
userAccesses = db.UserAccesses.Where(u => u.UserID == id)
.Include(i => i.User)
.Where(u => u.UserID == id)
.OrderBy(s => s.UserAcessID);
break;
case "Name_asc":
userAccesses = db.UserAccesses.Where(u => u.UserID == id)
.Include(i => i.User)
.Where(u => u.UserID == id)
.OrderBy(s => s.User.Name);
break;
case "Name_desc":
userAccesses = db.UserAccesses.Where(u => u.UserID == id)
.Include(i => i.User)
.Where(u => u.UserID == id)
.OrderBy(s => s.User.Name);
break;
default:
userAccesses = db.UserAccesses.Where(u => u.UserID == id)
.Include(i => i.User)
.Where(u => u.UserID == id)
.OrderBy(s => s.Access);
break;
}
}
return View(userAccesses.ToList());
}
如您所见,在注释行中,我执行一个返回带有用户名的列表的查询。
我的问题是:如何在que视图上访问此列表?
我把它放在我看来:
@Html.LabelFor(m => m.SelectedUserRoleId)
@Html.DropDownListFor(m => m.SelectedUserRoleId, Model.UserRoles)
但我无法使用" Model.something"。
TIA!
答案 0 :(得分:1)
使用视图所需的属性在Models
目录中创建一个模型类:
public class MyModelOfSomething
{
public IEnumerable<UserAccess> UserAccesses { get; set; }
public SelectList NamesToList { get; set; }
}
在控制器中填充该模型:
public ActionResult Index(string searchString, string sortOrder)
{
var model = new MyModelOfSomething();
...
model.NamesToList = new SelectList(NamesToListQ, "Value", "Text");
...
model.UserAccesses = userAccesses.ToList();
return View(model);
}
现在,您可以在视图中使用@Model.NamesToList
和@Model.UserAccesses
来访问这两者。