2017-01-23 48 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。谢谢! –