如何为MVC构建可枚举的视图模型?

时间:2013-12-21 03:44:22

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

我正在尝试为用户索引构建一个可枚举的ViewModel,但是当我构建项目时,我无法通过ViewModel进行枚举。有人可以帮我弄清楚如何正确构建我的ViewModel吗?

我的UserProfileIndexViewModel

public class UserProfileIndexViewModel
{
    public List<int> UserId { get; set; }
    public List<string> UserName { get; set; }
    public List<string> FirstName { get; set; }
    public List<string> LastName { get; set; }
    public List<string> Email { get; set; }
    public List<string> ClientName { get; set; }
}

我的控制器

public ActionResult Index()
{
    var viewModel = new UserProfileIndexViewModel();
    var users = db.UserProfiles;

    foreach (var user in users)
    {
        viewModel.UserId.Add(user.UserId);
        ProfileBase _userProfile = ProfileBase.Create(user.UserName);
        viewModel.FirstName.Add(Convert.ToString(_userProfile.GetPropertyValue("FirstName")));
        viewModel.LastName.Add(Convert.ToString(_userProfile.GetPropertyValue("LastName")));
        viewModel.Email.Add(db.User_Contacts.Find(user.UserName).Contact.Email);
        viewModel.ClientName.Add(db.User_Clients.Find(user.UserName).Client.Name);
    }
    return View(viewModel);
}

我的索引

@model IEnumerable<project.ViewModels.UserProfileIndexViewModel>
@{
    ViewBag.Title = "Index";
}
<p>@Html.ActionLink("Create New", "Create")</p>
<table class="table table-condensed">
    <tr>
        <th>@Html.ActionLink("Create", "Create", "User")</th>
        <th>Username</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
        <th>Client</th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            <a href="Details/@item.UserId" title="Details">Details</a>
            <a href="Edit/@item.UserId" title="Edit">Edit</a>
            <a href="Delete/@item.UserId" title="Delete">Delete</a>
        </td>
        <td>@item.UserName</td>
        <td>@item.FirstName</td>
        <td>@item.LastName</td>
        <td>@item.Email</td>
        <td>@item.ClientName</td>
    </tr>
}
</table>

1 个答案:

答案 0 :(得分:3)

每个用户个人资料都包含一个用户ID,用户名,名字等,因此模型应如下所示:

public class UserProfileIndexViewModel
{
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string ClientName { get; set; }
}

您的控制器操作应如下所示:

var users = db.UserProfiles;
var viewModel = new List<UserProfileIndexViewModel>();
foreach (var user in users)
{
    ProfileBase _userProfile = ProfileBase.Create(user.UserName);
    viewModel.Add(new UserProfileIndexViewModel {
        UserId = user.UserId,
        FirstName  = Convert.ToString(_userProfile.GetPropertyValue("FirstName")),
        LastName = Convert.ToString(_userProfile.GetPropertyValue("LastName")),
        Email = db.User_Contacts.Find(user.UserName).Contact.Email,
        ClientName = db.User_Clients.Find(user.UserName).Client.Name
    });
}
return View(viewModel);

然后你的观点就像现在一样。