C# 如何从模型中访问第二个iCollection并迭代该集合以获取其数据类型?
在我看来,有没有一种方法作为它的模型Exer_锻炼,来访问:Exer_Set_Pivot的深嵌套ICollection,并在foreach循环中获取其成员的数据类型? 头等舱:C# 如何从模型中访问第二个iCollection并迭代该集合以获取其数据类型?,c#,asp.net-mvc,model,C#,Asp.net Mvc,Model,在我看来,有没有一种方法作为它的模型Exer_锻炼,来访问:Exer_Set_Pivot的深嵌套ICollection,并在foreach循环中获取其成员的数据类型? 头等舱: public partial class Exer_Workout { public Exer_Workout() { this.Exer_Routine = new HashSet<Exer_Routine>(); } public int ObjID {
public partial class Exer_Workout
{
public Exer_Workout()
{
this.Exer_Routine = new HashSet<Exer_Routine>();
}
public int ObjID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Notes { get; set; }
public System.DateTime StartTime { get; set; }
public Nullable<System.DateTime> EndTime { get; set; }
public virtual ICollection<Exer_Routine> Exer_Routine { get; set; }
}
第二款车型第一款车型的一对多:
public partial class Exer_Routine
{
public Exer_Routine()
{
this.Exer_Set_Pivot = new HashSet<Exer_Set_Pivot>();
}
public int ObjID { get; set; }
public int WorkoutID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Notes { get; set; }
public Nullable<System.DateTime> StartTime { get; set; }
public Nullable<System.DateTime> EndTime { get; set; }
public virtual Exer_Workout Exer_Workout { get; set; }
public virtual ICollection<Exer_Set_Pivot> Exer_Set_Pivot { get; set; }
}
我的第三个模型类与我的第二个模型类有一对多:
public Exer_Set_Pivot()
{
this.Exer_Set = new HashSet<Exer_Set>();
}
public int ObjID { get; set; }
public int RoutineID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool Weight { get; set; }
public bool WeightUnit { get; set; }
public bool Reps { get; set; }
public bool Challenge { get; set; }
public bool ElapsedTime { get; set; }
public bool Distance { get; set; }
public bool DistanceUnit { get; set; }
public bool Speed { get; set; }
public bool SpeedUnit { get; set; }
public virtual Exer_Routine Exer_Routine { get; set; }
public virtual ICollection<Exer_Set> Exer_Set { get; set; }
}
我的局部视图,需要访问Exer_Set_Pivot成员并确定其日期类型,如果是类型BOOL,则将其设置为复选框:
@model Diabuddies.Models.Exer_Workout
<fieldset>
<legend>Set @ViewBag.SC </legend>
<table>
<tr>
<td>
Name:
</td>
<td>
@Html.TextBoxFor(x => x.Exer_RoutineList[Model.RoutineCount].Exer_SetPivotList[Model.SetCount].Name)
@* <input type="text" name="Workout.Exer_Routine[@ViewBag.RC].Exer_Set_Pivot[@ViewBag.SC].Name"> *@
</td>
</tr>
<tr>
<td>
Description:
</td>
<td>
@Html.TextBoxFor(x => x.Exer_RoutineList[Model.RoutineCount].Exer_SetPivotList[Model.SetCount].Description)
</td>
</tr>
<tr>
<td>Repeat Set:</td>
<td>
@Html.TextBoxFor(x => x.Exer_RoutineList[Model.RoutineCount].Exer_SetPivotList[Model.SetCount].RepeatSet, new { maxlength=3 })
</td>
</tr>
>>>>>HERE<<<<< DSFSADLFSDLFK DSL FKDSLFKLDSKF
@foreach (dfgdfgdfg)
{
@*This is where I am stuck. Need to get Exer_Set_Pivot's members and determine their type *@
<tr>
<td>
System.Collections.Generic.List
</td>
<td>
@(listitem.GetType()) and put it into checkbox, just need to get here.
</td>
</tr>
}
</table>
您可以通过反射实现所有这些,但这将导致一个非常可怕的代码。您最好访问视图模型的各个成员:
@foreach (Exer_Routine routine in Model.Exer_Routine)
{
<tr>
@foreach (Exer_Set_Pivot pivot in routine.Exer_Set_Pivot)
{
<td>
@Html.TextBoxFor(x => x.Name)
</td>
<td>
@Html.TextBoxFor(x => x.Description)
</td>
<td>
@Html.CheckBoxFor(x => x.Weight)
</td>
<td>
@Html.CheckBoxFor(x => x.WeightUnit)
</td>
...
}
</tr>
}
Exer_例程无法像这样访问。而且我只在类型为bool时创建复选框。你说它不可访问是什么意思?您的视图模型是Exer_锻炼,它有一个名为Exer_例程的属性,因此正是通过model.Exer_例程,您可以访问Exer_例程中的这个集合属性。var例程有效-我的错,谢谢。但是现在我可以深入到Exer\u Set\u Pivot,如何迭代该模型,以及如果system.type==bool,如何捕获?