我一直在努力在我的HomeController中遍历我的私有静态列表,以这种方式显示它显示学生和人员的列表。下面是我的循环应该是什么样子的一个例子。
[学生]姓名:John姓:Greenberg电邮:123 @ 123手机:123456789年龄:20
[人员]姓名:Rose姓:Marry Email:email @ email手机:123456789工人类型:永久学位:BED教育
[学生]姓名:Chaz姓:Brown电邮:chazz@gmail.com手机:123456789年龄:30
请帮我正确循环,下面是我试过编码的ContestantView
@model List<Assignment9_u14333393.Models.ContestantViewModel>
<div style="width:100%;height:auto; background-color:brown; padding-top:10px; padding-bottom:10px;">
<h2 style="text-align:center; color:white;">List of Contestants</h2>
</div>
.
<div class="members" >
<table>
@foreach (var temp in Model)
{
<div class="member">
[@temp.MemberType] Name:@temp.Name Surname:@temp.Surname Email: @temp.Email Cellphone:@temp.CellPhone
</div>
}
</table>
</div>
有关其他信息,我还有三个模型(StudentViewModel,PersonnelViewModel和ContestantViewModel)。 ContestantViewModel是我的父类,StudentViewModel和PersonnelViewModel是我的类,它继承了父类的数据成员和属性。
第一模特
public class ContestantViewModel
{
//Data members
private string mName;
private string mSurname;
private string mCellPhone;
private string mEmail;
private string mMemberType;
//Defeaut Constructor
public ContestantViewModel()
{
mName = "NoName";
mSurname = "NoSurname";
mCellPhone = "NoCellNumber";
mEmail = "NoEmail";
mMemberType = "NoMemberType";
}
//Constructor
public ContestantViewModel(string Name, string Surname, string CellPhone, string Email, string MemberType)
{
mName = Name;
mSurname = Surname;
mCellPhone = CellPhone;
mEmail = Email;
mMemberType = MemberType;
}
//Properties
public string Name
{
get { return mName; }
set { mName = value; }
}
public string Surname
{
get { return mSurname; }
set { mSurname = value; }
}
public string CellPhone
{
get { return mCellPhone; }
set { mCellPhone = value; }
}
public string Email
{
get { return mEmail; }
set {mEmail = value; }
}
public string MemberType
{
get; set;
}
}
2rd Model
public class PersonnelViewModel : ContestantViewModel
{
private string mWorkerType;
private string mDegree;
public PersonnelViewModel(string Name, string Surname, string CellPhone, string Email, string MemberType, string WorkerType, string Degree) : base (Name,Surname,CellPhone,Email, MemberType)
{
mWorkerType = WorkerType;
mDegree = Degree;
}
public PersonnelViewModel()
{
mWorkerType = "NoWorkerType";
mDegree = "NoDegree";
}
public string WorkerType
{
get { return mWorkerType;}
set { mWorkerType = value; }
}
public string Degree
{
get { return mDegree; }
set { mDegree = value; }
}
}
第三模特
public class StudentViewModel : ContestantViewModel
{
//Data members
private int mAge;
//D Constructor
public StudentViewModel()
{
mAge = 0;
}
//Constructor
public StudentViewModel(string Name, string Surname, string CellPhone, string Email, string MemberType, int Age) : base(Name, Surname, CellPhone, Email, MemberType)
{
mAge = Age;
}
//properties
public int Age
{
get { return mAge; } set { mAge = value; }
}
}
这是我的控制器
public class HomeController : Controller
{
// list to hold all my new members
private static List<ContestantViewModel> List = new List<ContestantViewModel>();
// GET: Home
public ActionResult Index()
{
return View();
}
// GET: Signup
public ActionResult Signup(string Name, string Surname, string Email, string Cellphone, string MemberType, int Age,string WorkerType,string Degree)
{
StudentViewModel Stundent = new StudentViewModel();
PersonnelViewModel Personnel = new PersonnelViewModel();
if (MemberType == "Student")
{
//creates instance
Stundent.Name = Name;
Stundent.Surname = Surname;
Stundent.Email = Email;
Stundent.CellPhone = Cellphone;
Stundent.MemberType = MemberType;
Stundent.Age = Age;
// Add data to list
List.Add(Stundent);
}
else
{
//creates instance
Personnel.Name = Name;
Personnel.Surname = Surname;
Personnel.Email = Email;
Personnel.CellPhone = Cellphone;
Personnel.MemberType = MemberType;
Personnel.WorkerType = WorkerType;
Personnel.Degree = Degree;
// Add data to list
List.Add(Personnel);
}
return View(List);
}
}
答案 0 :(得分:0)
将您的收藏放入其自己的视图模型中,而不是尝试使用此行引用它:
@model List<Assignment9_u14333393.Models.ContestantViewModel>
使用ContestantViewModel的集合创建一个新的视图模型ViewModelCollections。像
这样的东西List<ContestantViewModel> ContestantList
(您也可以添加其他人,例如您的学生和工作人员并重复使用该模型,即使出于某些目的,某些集合是空的)
然后用:
引用它@model ViewModelCollections
将所有参赛者放入收藏后,您就会非常接近当前的观看代码。
<div class="members">
<table id="contestantListTable">
<tbody>
@* Column Headers *@
<tr class="contestantListHeaders">
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone</th>
</tr>
@* Content Rows *@
@foreach (var temp in Model.ContestantList)
{
<tr>
<td>@temp.mName</td>
<td>@temp.mSurName</td>
<td>@temp.mEmail</td>
<td>@temp.mCellPhone</td>
</tr>
}
</tbody>
</table>
</div>
而不是这句话&#34;姓名:参赛者&#34; &#34;姓氏:#1&#34;等等
你将有一张桌子:
First Name | Last Name | Email etc etc
Contestant | #1 | ...
.
.
.