1
我想爲用戶索引構建一個可枚舉的ViewModel,但是當我構建項目時,我無法通過ViewModel枚舉。有人可以幫我弄清楚如何正確構建我的ViewModel?如何構建MVC的可枚舉視圖模型?
我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>
爲什麼所有屬性都是一個集合..? –
因爲我不知道我在做什麼。 – CIA
@CIA哈以及至少你的誠實:) –