Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带where条件的mvc 5 asp.net选择列表_C#_Asp.net Mvc_Linq - Fatal编程技术网

C# 带where条件的mvc 5 asp.net选择列表

C# 带where条件的mvc 5 asp.net选择列表,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,这是操作控制器中的选择列表 ViewBag.UserName = new SelectList(db.Users.Where(g => g.UserName == User.Identity.GetUserName()), "UserName", "UserName"); 我将其传递给视图: <div class="form-group"> @Html.LabelFor(model => model.UserName, "UserName",

这是操作控制器中的选择列表

ViewBag.UserName = new SelectList(db.Users.Where(g => g.UserName == User.Identity.GetUserName()), "UserName", "UserName");
我将其传递给视图:

 <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>

@LabelFor(model=>model.UserName,“UserName”,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownList(“UserName”,null,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.UserName,“,new{@class=“text danger”})
它不起作用了,埃罗:


LINQ to实体无法识别方法“System.String” GetUserName(System.Security.Principal.IIdentity)方法,并且 方法无法转换为存储表达式

 var username = User.Identity.GetUserName();
 ViewBag.UserName = new SelectList(db.Users.Where(g => g.UserName == username), "UserName", "UserName");

将第一行更改为:

string userName = User.Identity.GetUserName();
ViewBag.UserName = new SelectList(db.Users.Where(g => g.UserName == userName).ToList(), "UserName", "UserName");

为了执行查询并返回结果,必须在
的Where
后面添加
ToList()

将第一行更改为:

string userName = User.Identity.GetUserName();
ViewBag.UserName = new SelectList(db.Users.Where(g => g.UserName == userName).ToList(), "UserName", "UserName");

您必须在
Where
之后添加
ToList()
,才能执行查询并返回结果。

GetUserName无法转换为表达式。像这样试试

var username = User.Identity.GetUserName();
ViewBag.UserName = new SelectList(db.Users.Where(g => g.UserName == username), "UserName", "UserName");

还有可能像Haitham建议的那样添加.ToList()。

GetUserName无法转换为表达式。像这样试试

var username = User.Identity.GetUserName();
ViewBag.UserName = new SelectList(db.Users.Where(g => g.UserName == username), "UserName", "UserName");

还有可能像Haitham建议的那样添加.ToList()。

linq/lambda表达式无法识别
GetUserName
函数, 您必须在单独的变量中读取它,并在linq查询/lambda表达式中使用该变量

 var username = User.Identity.GetUserName();
 ViewBag.UserName = new SelectList(db.Users.Where(g => g.UserName == username), "UserName", "UserName");

linq/lambda表达式无法识别
GetUserName
函数, 您必须在单独的变量中读取它,并在linq查询/lambda表达式中使用该变量

 var username = User.Identity.GetUserName();
 ViewBag.UserName = new SelectList(db.Users.Where(g => g.UserName == username), "UserName", "UserName");

LINQ to Entities无法识别方法“System.String GetUserName(System.Security.Principal.IIIdentity)”方法,并且无法将此方法转换为存储表达式。请首先将
User.Identity.GetUserName()
放入变量,并确保其具有值,我已将我对Entities的回答设置为无法识别“System.String GetUserName(System.Security.Principal.IIIdentity)”方法,并且无法将此方法转换为存储表达式。请首先将
User.Identity.GetUserName()
放入变量,并确保其具有值,我已经回答了您不能在任何Linq表达式中使用函数调用
User.Identity.GetUserName()
。您可以做的是首先将
User.Identity.GetUserName()
的值获取到一个变量中,然后在linq表达式中使用该变量。您不能在任何linq表达式中使用函数调用
User.Identity.GetUserName()
。您可以做的是首先将
User.Identity.GetUserName()
的值获取到一个变量中,然后在linq表达式中使用该变量