我使用ASP.net MVC和个人用户帐户。我想要一个页面,显示所有数据库记录,其中字段" user"等于帐户的电子邮件地址。我使用脚手架来创建所有东西,我只需要以某种方式添加一个where子句,它只显示特定用户的记录。我尝试在下面的控制器中。它编译,并且Exhibit5 / Index /按预期提示用户登录屏幕。但是,单击LogIn后,我找不到网络路径错误。
型号:
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; }
}
}
控制器:
public class Exhibit5Controller : Controller
{
private WebApplication1Context db = new WebApplication1Context();
[Authorize]
// GET: Exhibit5
public ActionResult Index()
{
var ex5 = db.Exhibit5.Where(d => d.User == User.Identity.GetUserName()).ToList();
return View(ex5);
}
答案 0 :(得分:0)
应该是:
var userName = User.Identity.GetUserName();
var ex5 = db.Exhibit5.Where(u => u.User == userName )
.Select(y => y.User);
答案 1 :(得分:0)
<强>快速修复强>
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()
。通过将其从实体框架中删除(种类)这个:
SELECT * FROM Exhibit5 WHERE User = <User Name>