1
新來的MVC和#2很抱歉因沒有足夠的聲譽,發佈圖片...MVC列表框選定值
試圖使具有預選值
我的SQL數據庫列表框: http://i.imgur.com/bcdXyqE.png
我的實體框架 http://i.imgur.com/wYWXuAq.png
我的控制器
public ActionResult AccessRights(int id)
{
var user = db.User.Find(id);
var roles = db.Role;
var newList = new List<SelectListItem>();
foreach (var role in roles)
{
newList.Add(
new SelectListItem()
{
Value = role.Id.ToString(),
Text = role.RoleName,
Selected = user.Role.Contains(role)
}
);
}
ViewBag.x = new SelectList(newList, "Value", "Text");
ViewBag.Roles = new SelectList(db.Role, "Id", "RoleName", user.Role);
return View(user);
}
我查看
<p>try1:</p>
@Html.ListBox("Roles", null, new { @class = "form-control", @size = 6, @style = "height: initial" })
<p>try2:</p>
@Html.ListBox("x", null, new { @size = 6, @style = "height: initial" })
非2次的嘗試使得具有預選值?