身份验证在_LogOnPartial.cshtml中写入动态数据

时间:2013-12-12 22:18:27

标签: asp.net-mvc-3 c#-4.0 razor

我是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") ]
}

1 个答案:

答案 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 
          }
    }