.Net MVC - 访问自定义标识用户属性

时间:2018-02-28 21:19:01

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

我在IdentityModel中添加了一些自定义字段到IdentityUser(例如名字)但是我在浏览中访问这些字段时遇到了问题。它们已通过迁移添加到数据库中,我已将信息放入其中。

这是我的模特 -

public class ApplicationUser : IdentityUser
{
    [Required]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "Surname")]
    public string Surname { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}`

在视图中我创建了一个表来查看此信息

<table class=" table table-bordered table-hover">
<tr>
    <th class="c">
        First Name
    </th>
    <th class="c">
        Surname
    </th>
</tr>
@foreach (var item in Model)
{
    <tr>
        <td class="c">
            @item.FirstName
        </td>
        <td class="c">
            @item.Surname
        </td>   
    </tr>
}

我在视图页面的顶部调用了模型

@model IEnumerable<Microsoft.AspNet.Identity.EntityFramework.IdentityUser>    

然而,这是我遇到错误的地方。对于@item。它无法识别我的自定义字段。我得到的具体错误是 -

&#39; IdentityUser&#39;不包含&#39; FirstName&#39;的定义没有扩展方法&#39; FirstName&#39;接受类型&#39; IdentityUser&#39;的第一个参数。可以找到。

我不确定为什么会出现这个错误,如果有人能提供帮助,我将不胜感激。

由于

1 个答案:

答案 0 :(得分:0)

尝试将模型更改为自定义类:

@model IEnumerable<YourNamespace.ApplicationUser> 
相关问题