如何从模型访问第二个iCollection并迭代该集合以获取其数据类型?

时间:2014-01-29 21:52:10

标签: c# asp.net-mvc model

在我看来,是否有一种方法可以访问Exer_Set_Pivot的深层嵌套ICollection,并在foreach循环中获取其成员的数据类型? 第一个模型类:

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; }
}

我的第三个Model Class,我的第二个模型类有一对多:

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>

1 个答案:

答案 0 :(得分:0)

你可以用反射实现所有这些,但这会导致一个非常可怕的代码。您最好访问视图模型的各个成员:

@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>
}