如何访问在布局文件中添加到ApplicationUser的模型属性 [英] How to access Model Property added to ApplicationUser in Layout file

查看:153
本文介绍了如何访问在布局文件中添加到ApplicationUser的模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ASP.Net MVC Web开发的初学者.我想知道如何访问布局文件中ApplicationUser模型中添加的其他属性?

I am a beginner in ASP.Net MVC web development. And I want to know how can I access extra property added in my ApplicationUser model in layout file?

到目前为止,如果必须访问剃刀文件中的属性,可以在Razor文件顶部添加@model myModel,然后可以通过@Model.MyProperty访问该属性.

As of now if I have to access a property in the razor file I add @model myModel at top of Razor file and then I am able to access the property by @Model.MyProperty.

假设我在Application User中添加了FirstName属性,如下所示:

Suppose I added FirstName property in my Application User like below:

public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
    }

现在在我的登录部分中,我有下面的代码.

Now in my Login Partial I have below Code.

@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>
            @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>
    }
}

假设我还想显示名字而不是User.Identity.GetUserName().

Suppose I also want to display FirstName instead of User.Identity.GetUserName() how can I do so.

下面是用户在注册过程中看到的RegisterViewModel.

Below is the RegisterViewModel which user will see during registration.

 public class RegisterViewModel
    {
        // rest property removed for brevity
        [Required]
        public string FirstName { get; set; }
    }

现在,当返回视图时,RenderBody()是渲染与该控制器动作相关的实际CSHTML文件的地方.但是在我的场景中,我需要在所有用户都通用的布局文件中访问applicationUser的属性.请引导我.

Now when a view is returned, the RenderBody() is the place where the actual CSHTML file related to that controller action gets rendered. But in my scenario I need to access the property of applicationUser in layout file which is common for all. Please guide me.

我可以做以下事情吗? User.Identity.GetFirstName 还是类似的东西?

Can I do something like: User.Identity.GetFirstName or something along these lines?

推荐答案

创建一个新的控制器,它将作为应用程序中所有控制器的基类(假设您已经在用户实例中保存了名字)

Create one new controller that will be base class for all all the controller in your application (Assuming that you have already saved firstname in user instance)

 public class ApplicationBaseController : Controller
        {
            protected override void OnActionExecuted(ActionExecutedContext filterContext)
            {
                if (User != null)
                {
                    var context = new ApplicationDbContext();
                    var username = User.Identity.Name;

                    if (!string.IsNullOrEmpty(username))
                    {
                        var user = context.Users.SingleOrDefault(u => u.UserName == username);

                        ViewData.Add("firstName", user.FirstName);
                    }
                }
                base.OnActionExecuted(filterContext);
            }

            }

例如,HomeController继承如下的ApplicationBaseController:

For instance the HomeController inherits ApplicationBaseController as below:

  public class HomeController : ApplicationBaseController

现在使用以下代码更改登录部分:

Now change the login partial with below code :

@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>
           @Html.ActionLink("Hello " + (ViewData["firstName"]) + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>
    }
}

这篇关于如何访问在布局文件中添加到ApplicationUser的模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆