function DisplayADSearchResults(result) {
var table = "<table id='myTable'>" +
"<tr>" +
"<th style='width:20px;'> </th>" +
"<th style='width:100px;'><b>First Name</b></th>" +
"<th style='width:100px;'><b>Last Name</b></th>" +
"<th style='width:200px;'><b>Email</b></th>" +
"<th style='width:200px;'><b>Login Name</b></th>" +
"<th style='width:200px;'><b>Role</b></th>" +
"</tr>";
for (var counter = 0; counter < result.length; counter++) {
var chkId = 'chkUserSearch' + counter;
var fId = 'fUserSearch' + counter;
var lId = 'lUserSearch' + counter;
var eId = 'eUserSearch' + counter;
var unid = 'unUserSearch' + counter;
var myData = "<tr>" +
"<td><input type='checkbox' id='" + chkId + "' /></td>" +
"<td id='" + fId + "' class='searchFirstName'>" + result[counter].FirstName + "</td>" +
"<td id='" + lId + "' class='searchLastName'>" + result[counter].LastName + "</td>" +
"<td id='" + eId + "' class='searchEmail'>" + result[counter].EmailAddress + "</td>" +
"<td id='" + unid + "' class='searchUn'>" + result[counter].LoginName + "</td>" +
"<td id='" + unid + "' class='searchUn'>" + ddlStaffCategory + "</td>" + //i want do display my "ddlStaffCategory" drop dropdownlist in this column.
"</tr>";
table += myData;
}
table += "</table>";}
我有上面的函数,用于根据搜索结果动态创建 html 表。在我的 Razor View 中,我有一个已创建并填充了值的下拉列表。
有人知道如何在上面表格的最后一列中为每一行显示该下拉列表吗?
下面是下拉列表源。
@Html.DropDownList("ddlStaffCategory", new SelectList(ViewBag.CategoryList as System.Collections.IEnumerable, "Value", "Text"), new { @class = "form-control form-control-dropdown-medium" })
更新
我已经尝试了@ADyson的建议,如下所示(最后两列),但这只是将html打印为纯文本而不是下拉列表控件
var searchData = "<tr>" +
"<td><input type='checkbox' id='" + chkId + "' /></td>" +
"<td id='" + dId + "' class='searchDomain'>" + userDomain + "</td>" +
"<td id='" + fId + "' class='searchFirstName'>" + result[counter].FirstName + "</td>" +
"<td id='" + lId + "' class='searchLastName'>" + result[counter].LastName + "</td>" +
"<td id='" + eId + "' class='searchEmail'>" + result[counter].EmailAddress + "</td>" +
"<td id='" + unid + "' class='searchUn'>" + result[counter].LoginName + "</td>" +
"<td id='" + unid + "' class='searchUn'>" + "@Html.DropDownList(\"ddlStaffCategory\", new SelectList(ViewBag.CategoryList as System.Collections.IEnumerable, \"Value\", \"Text\"), new { @class = \"form-control form-control-dropdown-medium\"})" + "</td>" +
"<td>@Html.DropDownList(\"ddlStaffCategory\", new SelectList(ViewBag.CategoryList as System.Collections.IEnumerable, \"Value\", \"Text\"), new { @class = \"form-control form-control-dropdown-medium\"})</td>" +
"</tr>";
最佳答案
@Html.DropDownList(...)
只是生成类似的东西:
<select id="SelectId" name="SelectName">
<option value="Opt1">Option 1</option>
<option value="Opt2">Option2</option>
...
</select>
将你的 Razor 助手放入 <td></td>
中你应该没问题
var myData =
"<tr>" +
"<td><input type='checkbox' id='" + chkId + "' /></td>" +
"<td id='" + fId + "' class='searchFirstName'>" + result[counter].FirstName + "</td>" +
"<td id='" + lId + "' class='searchLastName'>" + result[counter].LastName + "</td>" +
"<td id='" + eId + "' class='searchEmail'>" + result[counter].EmailAddress + "</td>" +
"<td id='" + unid + "' class='searchUn'>" + result[counter].LoginName + "</td>" +
"<td id='" + unid + "' class='searchUn'>@Html.DropDownList("ddlStaffCategory", new SelectList(ViewBag.CategoryList as System.Collections.IEnumerable, "Value", "Text"), new { @class = "form-control form-control-dropdown-medium" })</td>" +
"</tr>";
关于javascript - 如何将下拉列表复制或插入到 html 表格列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46605974/