2014-01-29 42 views
0

有沒有一種方法,在我看來,它有它的模型Exer_Workout,訪問的深層嵌套的ICollection:Exer_Set_Pivot,並獲得其成員的數據類型在foreach循環? 第一個模型類:如何從模型中訪問第二個iCollection並迭代該集合以獲取其數據類型?

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

第二個模型(一個從1模型很多):

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成員和確定它們的類型日期,並且如果它們的類型(布爾)設置它們爲複選框

我的局部視圖:

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

回答

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> 
} 
+0

Exer_Routine不能像這樣訪問。如果type是bool,我也只創建複選框。 – BriOnH

+0

你是什麼意思,它無法訪問?您的視圖模型是'Exer_Workout'其中有一個叫做'Exer_Routine'屬性,因此它與'Model.Exer_Routine',你會訪問這個集合屬性也正是。 –

+0

Exer_Routine中的var例程工作 - 我的不好,謝謝。但是現在我可以深入到Exer_Set_Pivot,我該如何遍歷該模型,並且如果system.type == bool,catch? – BriOnH

相關問題