在视图中显示ASP MVC用户表中的已添加字段

时间:2017-01-20 18:41:14

标签: asp.net-mvc asp.net-mvc-4

我首先使用代码并将字段添加到数据库aspnetusers中,当您注册时,一切正常并保存到数据库中。但我无法在视图中显示新字段。我只能查看已经预装ASP的字段。第一个名字在DB中就好了,不知道如何在视图中查看它。 Intellisense不会加载添加的字段

 @model IEnumerable<MyProject.Models.ApplicationUser >
 @{
  ViewBag.Title = "Home Page";
   }
  <h2>Current users</h2>

  <table class="table1" id="sort">
    <tr>
       <th>
        @Html.ActionLink("Email:", "Index", new { sortOrder =    ViewBag.AddressSortParm })
    </th>
    <th>
        @Html.ActionLink("First Name:", "Index", new { sortOrder = ViewBag.StatusSortParm })
       </th>
       <th>
        @Html.ActionLink("Role:", "Index", new { sortOrder = ViewBag.DateSortParm })
       </th>
       <th>
        Owner Information:
       </th>
       <th></th>
    </tr>

  @foreach (var user in Model)
  {
    <tr>
        <td>
            @user.UserName
        </td>
        <td>
            @user.FirstName
        </td>
        <td>
            @user.Email
        </td>
        <td>
        </td>
    </tr>
}

  

MY控制器

    ApplicationDbContext context = new ApplicationDbContext();
    public ActionResult Index()
    {
        var context = new IdentityDbContext();
        var users = context.Users.ToList();
        return View(users);
    }

1 个答案:

答案 0 :(得分:0)

错误是因为视图的第一行引用了Microsoft.AspNet.Identity,而不是您添加了firstname属性的视图模型。

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

应改为:

 @model IEnumerable<ApplicationUser>

ApplicationUser类似于

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

然后从Action中返回Users as ApplicationUser。

或者您可能只需要将ViewModel用作添加自定义属性的@model。