2017-01-23 26 views
0

我需要基於模型中的對象顯示45個文本框。我想知道如果我能做到這一個循環中,而不是編碼它的44倍(這不會是有史以來最糟糕的事情......只是爲了尋找一個快捷方式。)在For循環中動態地將模型對象分配給TextBoxFor

@for (int y = 0; y < 44; y++) 
{ 
    <div class="row"> 
     @Html.Label(y + 1 + " Years Old: ", new { @class = "col-md-3 control-label" }) 
     <div class="input-group input-group-sm col-md-9"> 
      <span class="input-group-addon">$</span> 
      @Html.TextBoxFor(m => Model.LeadPricingModel.MinYearBuilt_0, new { @class = "form-control input-sm", style = "width:100px;" }) 
     </div> 
    </div> 
} 

標籤的偉大工程。但是,我想要做的是根據y根據MinYearBuilt_0製作_0。不知道如何做到這一點。

回答

1

試試這個。你將不得不使MinYearBuilt數組或列表(但如果它已經不是)。確保它是否是一個數組,以將其初始化爲足夠大的45個對象。

@for (int y = 0; y < 44; y++) 
{ 
    <div class="row"> 
    @Html.Label(y + 1 + " Years Old: ", new { @class = "col-md-3 control-label" }) 
    <div class="input-group input-group-sm class=" col-md-9""> 
     <span class="input-group-addon">$</span> 
     @Html.TextBoxFor(m => Model.LeadPricingModel.MinYearBuilt[y], new { @class = "form-control input-sm", style = "width:100px;" }) 
    </div> 
</div> 
} 
+0

好吧,好主意。是的,這將需要重新處理對象,因爲它目前不是數組。我只是將整個對象作爲JSON blob保存在數據庫中,所以我必須找出哪個更快......重新處理對象,或者只是編寫45 TextBoxFor's。謝謝! –