这是 Action Controller 中的选择列表,
ViewBag.UserName = new SelectList(db.Users.Where(g => g.UserName == User.Identity.GetUserName()), "UserName", "UserName");
我正在传递给 View :
<div class="form-group">
@Html.LabelFor(model => model.UserName, "UserName", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("UserName", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
它不工作错误:
LINQ to Entities does not recognize the method 'System.String GetUserName(System.Security.Principal.IIdentity)' method, and this method cannot be translated into a store expression.
最佳答案
GetUserName 无法转换为表达式。试试这个
var username = User.Identity.GetUserName();
ViewBag.UserName = new SelectList(db.Users.Where(g => g.UserName == username), "UserName", "UserName");
也许像 Haitham 建议的那样添加 .ToList()。
关于c# - 带where条件的mvc 5 asp.net selectlist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40715754/