我是ASP.net的初学者,我有使用Razor引擎的MVC 3 C#项目。如何在视图_LogOnPartial.cshtml中替换 @ User.Identity.Name
从db上下文EF获取属性例如,在身份验证后,我必须显示:Welcome(customer_Name)[LogOff]
这是_LogOnPartial.cshtml的代码:
@if (Request.IsAuthenticated)
{
<text>Welcome <strong>@User.Identity.Name</strong>!
[ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
}
else {
@:[ @Html.ActionLink("Log On", "LogOn", "Account") ]
}
答案 0 :(得分:1)
在数据层中创建一个方法并在视图中调用它
查看
@if (Request.IsAuthenticated)
{
<text>Welcome <strong>@YOURNAMESPACE.getName(User.Identity.Name)</strong>!
[ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
}
else {
@:[ @Html.ActionLink("Log On", "LogOn", "Account") ]
}
数据层
public string getName(string username)
{
using (var context = new yourdbcontext(tenantId))
{
return context.Users.Where(a => a.UserName == username).Select(a => a.customer_Name ).SingleO
}
}