2011-01-05 52 views
1

我使用SQL Server的所有默认ASP.NET窗体身份验证表(aspnet_Profile,aspnet_Roles,aspnet_Users等)。我还为我的数据库添加了一个Employees表,该表包含UserId上的FK到aspnet_Users以创建一对一关系(UserId是Employees中的PK)。 Employees表包含FirstName,LastName等的列。这是我为用户保留附加信息的一种方式。创建一个模型,并将其显示在一个网格中ASP.NET MVC

我想要一个网格显示我的员工列表。它需要包含以下信息:从aspnet_Membership

  • 从aspnet_Users

    1. 名和姓Employees表
    2. 用户名
    3. 电子邮件使用Roles.GetRolesForUser(username)角色的逗号分隔列表中的用户在发现

    这是我想用我的浏览页面做什么:

    <%@ Page Language="C#" 
        Inherits="System.Web.Mvc.ViewPage<IEnumerable<WebUI.Models.IndexModel>>" %> 
    
    <table> 
        <tr> 
         <th></th> 
         <th>Employee</th> 
         <th>Username</th> 
         <th>E-mail Address</th> 
         <th>Roles</th> 
        </tr> 
        <% foreach (var item in Model) { %> 
         <tr> 
          <td></td> 
          <td><%: item.FirstName %> <%: item.LastName %></td> 
          <td><%: item.Username %></td> 
          <td><%: item.Email %></td> 
          <td><%: item.Roles %></td> <!-- i.e., "Admin, Sales Rep" --> 
         </tr> 
        <% } %> 
    </table> 
    

    这里是我的模型:

    public class IndexModel 
    { 
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
        public string Username { get; set; } 
        public string Email { get; set; } 
        public string[] Roles { get; set; } 
    } 
    

    而且我的控制器:

    public ActionResult Index() 
    { 
        return View(); 
    } 
    

    我的问题是,我不知道如何加载我的模型与数据,并把它传递给我的看法。我对MVC有点新,而且我已经做了一些搜索,但没有运气,我想我只是在寻找错误的东西。

    那么我怎样才能加载我的模型与来自这些不同的表的数据?

    编辑:我应该提到我使用实体框架,所以我可以轻松地从雇员表中拉出所有数据。

  • 回答

    1

    你是如此接近:

    public ActionResult Index() 
    { 
        IEnumerable<IndexModel> model = new List<IndexModel>(); // or from your EF Repository 
    
        return View(model); 
    } 
    

    个人而言,我将你的模型对象改变这一点,并相应地更新你的视野:

    public class IndexModel 
    { 
        IList<UserInfo> Users { get; set; } 
    } 
    
    public class UserInfo // or whatever makes the most sense 
    { 
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
        public string Username { get; set; } 
        public string Email { get; set; } 
        public string[] Roles { get; set; } 
    } 
    
    0

    你通过模型到您的视图控制器:

    public ActionResult Index() 
    { 
        List<IndexModel> items = new List<IndexModel>(); 
        // fill out list of items 
    
        return View(items); 
    } 
    
    相关问题