如何在MVC中添加网格中的html.dropdown?

时间:2017-04-14 06:39:16

标签: asp.net-mvc model-view-controller

<div style="margin-left:27px; margin-bottom:20px;">
            <h2>Email Scheduler</h2>
            <div id="dvtblCustomer" class="table-responsive">
                <table class="table-bordered col-offset-12">
                    <thead>
                        <tr class="label-primary">
                            <th style="padding:5px 15px;">
                                Customer Designation
                                 @Html.DropDownList("CustomerDesignation", new SelectList(Model., "Value", "Text"), new { id = "CustomerDesignationDDL", name = "CustomerDesignationDDL" })
                            </th> 
                        </tr>
                    </thead>

                    @foreach (var item in Model)
                    {
                        <tr style="text-align:center">
                            <td>
                                @item.CustomerDesignation
                            </td>
                        </tr>
                    }

            </table>
        </div>
    </div>

这是我的webgrid。我想在表格标题中添加dropdownlist以进行表过滤

1 个答案:

答案 0 :(得分:1)

System.Web.Mvc.SelectListItem是用于此目的的汇编类。

鉴于您的CustomerDesignation模型是一个指定列表,然后从中构建一个List<SelectListItem>,其中designations是您的数据库/文件驱动的客户指定列表:

List<SelectListItem> designationList = designations.Select(d => new SelectListItem { 
  Text = d.CustomerDesignationDDL, 
  Value = d.CustomerDesignationDDL 
}).ToList();

SelectItemList namesList绑定到视图模型

Model.Designations = designationList;

在视图中,可以填充相同的下拉列表:

@Html.DropDownListFor(model => model.Designations, Model.Designations)