我试图用我的 ASP.NET 用户数据库的所有用户填充我的 Html.Listbox。
我创建了一个看起来像这样的 ViewModel:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
namespace SkyLearn.Areas.Users.Models
{
public class UsersViewModel
{
MembershipUserCollection membershipuserscollection { get; set; }
public List<MembershipUser> membershipuserslist { get; set; }
public List<SelectListItem> AvailableUsers { get; set; }
public UsersViewModel()
{
availableUsers = new List<SelectListItem>();
}
}
}
一个如下所示的 Actionresult:
public ActionResult Index()
{
var users = Membership.GetAllUsers().Cast<MembershipUser>();
UsersViewModel model = new UsersViewModel();
foreach (MembershipUser user in users)
{
SelectListItem selectuser = new SelectListItem();
selectuser.Text = user.UserName;
selectuser.Value = user.UserName;
model.AvailableUsers.Add(selectuser);
}
return View(model);
}
在我看来,我尝试像这样填充列表:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SkyLearn.Areas.Users.Models.UsersViewModel>" %>
<%= Html.ListBox("AvailableUsers", Model.AvailableUsers)%>
当我运行代码时,出现以下错误:
CS1579:foreach 语句无法对“SkyLearn.Areas.Users.Models.UsersViewModel”类型的变量进行操作,因为“SkyLearn.Areas.Users.Models.UsersViewModel”不包含“GetEnumerator”的公共定义