C# ASP.net MVC:显示特定用户的结果
我将ASP.NETMVC用于个人用户帐户。我想要一个显示所有数据库记录的页面,其中字段“user”等于帐户的电子邮件地址。我使用脚手架来创建一切,我只需要添加一个where子句,它只显示特定用户的记录。我在下面的控制器中尝试过。它按照预期编译并显示5/索引/提示用户登录屏幕。但是,单击“登录”后,我得到一个“未找到网络路径”错误 型号:C# ASP.net MVC:显示特定用户的结果,c#,asp.net-mvc,C#,Asp.net Mvc,我将ASP.NETMVC用于个人用户帐户。我想要一个显示所有数据库记录的页面,其中字段“user”等于帐户的电子邮件地址。我使用脚手架来创建一切,我只需要添加一个where子句,它只显示特定用户的记录。我在下面的控制器中尝试过。它按照预期编译并显示5/索引/提示用户登录屏幕。但是,单击“登录”后,我得到一个“未找到网络路径”错误 型号: public partial class Exhibit5 { public int ID { get; set; } public strin
public partial class Exhibit5
{
public int ID { get; set; }
public string User { get; set; }
public string Name { get; set; }
public Nullable<int> EmployeeID { get; set; }
public string ProposedTitle { get; set; }
public string Department { get; set; }
public Nullable<decimal> AnnualizedSalary { get; set; }
public string PayGrade { get; set; }
public Nullable<decimal> MktPay { get; set; }
public Nullable<decimal> RangeMin { get; set; }
public Nullable<decimal> RangeMid { get; set; }
public Nullable<decimal> RangeMax { get; set; }
public Nullable<decimal> RangePen { get; set; }
public Nullable<decimal> CompaRatio { get; set; }
public Nullable<decimal> BelowMin { get; set; }
public Nullable<decimal> AboveMax { get; set; }
}
应该是:
var userName = User.Identity.GetUserName();
var ex5 = db.Exhibit5.Where(u => u.User == userName )
.Select(y => y.User);
快速修复:
public ActionResult Index()
{
var userName = User.Identity.GetUserName();
var ex5 = db.Exhibit5.Where(d => d.User == userName).ToList();
return View(ex5);
}
原因是User.Identity.GetUserName()
在Where
中不起作用,因为实体框架需要将Where
代码转换为SQL查询,它不知道如何执行User.Identity.GetUserName()
。通过将其从实体框架中拉出,可以(某种程度上)执行以下操作:
从用户=
您调试代码了吗?它是否转到登录的post操作?用户名和密码验证正确吗?^。另外,Identity
是否确实为此请求设置了?另外,是否确实为此操作创建了视图?在调试过程中,我现在收到以下错误:System.NotSupportedException:“LINQ to Entities无法识别方法”System.String GetUserId(System.Security.Principal.IIdentity)“,这个方法不能被翻译成商店表达式。“我用这个词算出了:谢谢!
public ActionResult Index()
{
var userName = User.Identity.GetUserName();
var ex5 = db.Exhibit5.Where(d => d.User == userName).ToList();
return View(ex5);
}
SELECT * FROM Exhibit5 WHERE User = <User Name>