2017-05-01 14 views
1

從我的MVC 5視圖中的以下代碼中,我動態地構建了標籤和文本框控件,但我需要將它們格式化爲表格,所以我不確定如何才能做到這一點。如何格式化MVC中的動態控件5

@using InFlowConvertWeb.WebUI.Models 
    @model InFlowConvertWeb.WebUI.Models.SearchControlListViewModel 
    @{ 
     ViewBag.Title = "List"; 
    } 

    @using (Html.BeginForm()) 
    { 
     int searchControlIndex = 0; 

     foreach (SearchControl searchControl in Model.SearchControls) 
     { 
      switch (searchControl.ControlType) 
      { 
       case SearchControl.ControlTypes.TextBox: 
        { 
         <div class="form-group" style="margin-left: 15px"> 
          @Html.Label(searchControl.FieldName, 
           new { @class = "col-md-12 control-label" }) 

          @Html.TextBoxFor(
           x => x.SearchControls[searchControlIndex].SearchValue) 

          @Html.HiddenFor(x => x.SearchControls[searchControlIndex].DataTable) 
          @Html.HiddenFor(x => x.SearchControls[searchControlIndex].FieldName) 
         </div> 

         break; 
        } 
      } 

      searchControlIndex += 1; 
     } 

     <div class="col-md-2"> 
      <h2> 
       <input type="submit" value="Submit Selections" /> 
      </h2> 
     </div> 

將不勝感激任何建議,

鮑勃

+0

你只是問如何做一個''

在HTML? – David

回答

0

嘗試這個例子:

@using InFlowConvertWeb.WebUI.Models 
    @model InFlowConvertWeb.WebUI.Models.SearchControlListViewModel 
    @{ 
     ViewBag.Title = "List"; 
    } 

    @using (Html.BeginForm()) 
    { 
<table id="dataTable" class="table table-bordered table-hover"> 
    <tbody> 
     @{int searchControlIndex = 0;} 

     @foreach (SearchControl searchControl in Model.SearchControls) 
     { 
      switch (searchControl.ControlType) 
      { 
       case SearchControl.ControlTypes.TextBox: 
        { 
        <tr> 
         <td> 
          @Html.Label(searchControl.FieldName,         new { @class = "col-md-12 control-label" }) 
         </td> 
         <td> 
          @Html.TextBoxFor(x =>x.SearchControls[searchControlIndex].SearchValue) 

          @Html.HiddenFor(x =>x.SearchControls[searchControlIndex].DataTable) 
          @Html.HiddenFor(x =>x.SearchControls[searchControlIndex].FieldName) 
         </td> 
       </tr> 
         break; 
        } 
      } 

      searchControlIndex += 1; 
     } 
    </tbody> 
</table> 

<div class="col-md-2"> 
    <h2> <input type="submit" value="Submit Selections" /> </h2> 
</div> 
}