开发者

Creating a model, and displaying it in a grid in ASP.NET MVC

I'm using all the default ASP.NET forms authentication tables for SQL Server (aspnet_Profile, aspnet_Roles, aspnet_Users, etc.). I've also added an Employees table to my database, which contains a FK to aspnet_Users on UserId to create a one-to-one relationship (UserId is the PK in Employees). The Employees table contains columns for FirstName, LastName, etc. It's a way for me to keep additional information for the users.

I'd like to have a grid display my list of Employees. It needs to contain this information:

  1. FirstName and LastName from the Employees table
  2. UserName from aspnet_Users
  3. Email from aspnet_Membership
  4. A comma-delimited list of roles the user is in, found using Roles.GetRolesForUser(username)

Here's what I'd like to do with my View page:

<%@ 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>

Here's my model:

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; }
}

And my controller:

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

My problem is, I don't know how to load my model with that data and pass it on to my view. I'm a little开发者_运维问答 new to MVC, and I've done some searching around with no luck, I think I'm just searching for the wrong thing.

So how can I load up my model with the data from those different tables?

EDIT: I should mention I'm using Entity Framework, so I can easily pull all the data from the Employees table.


you're so close:

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

    return View(model);
}

personally I would change your model object to this and update your View accordingly:

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; }
}


You pass your model into your view in your controller:

public ActionResult Index()
{
    List<IndexModel> items = new List<IndexModel>();
    // fill out list of items

    return View(items);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜