如何格式化MVC 5中的动态控件

时间:2017-05-01 14:57:11

标签: razor asp.net-mvc-5

在我的MVC 5 View的以下代码中,我正在动态构建标签和文本框控件,但我需要在表格中对它们进行格式化,所以我不确定如何做到这一点。

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

非常感谢任何建议,

鲍勃

1 个答案:

答案 0 :(得分: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>
}