名称和ID存储在数据库中。我想显示从数据库到ViewPage下拉列表的所有名称和ID。这里我只在下拉列表中显示了Name列。是否有可能在单个下拉列表中显示两列?请让我知道解决方案。
控制器:
var Employe = (from table in dc.tbl_EmployeeDetails
select table.Employee_Name).ToList();
ViewData["empName"] = Employe ;
var department = (from table in dc.tbl_EmployeeDetails
select table.Department).ToList();
ViewData["dept"] = department;
查看
<select id="reportto" disabled="disabled" font-size:14px">
@foreach(var person in ViewData["empName"] as List<string>)
{
if(@Model.employee!=@person)
{
<option>@person</option>
}
}
</select>
这里我只在下拉列表中显示了名称。如何在单个下拉列表中显示名称和部门?
答案 0 :(得分:1)
请做一件事。
请使用SelectListItem作为绑定下拉列表。
List<SelectListItem> lstLocation = new List<SelectListItem>();
SqlParameter[] parameters = { new SqlParameter("@ID", ID),new
SqlParameter("@Name",Name)};
DataSet dsLocation = SqlHelper.ExecuteDataset(CommandType.StoredProcedure, "GetPersonalInfo", parameters);
if (dsLocation != null && dsLocation.Tables.Count > 0 && dsLocation.Tables[0].Rows.Count > 0)
{
lstLocation = (from drLocation in dsLocation.Tables[0].AsEnumerable()
select new SelectListItem { Text = `drLocation["PersonID"].ToString() + ' - '+ drLocation["PersonName"].ToString(), Value = drLocation["PersonID"].ToString() }).ToList();`
}
并将此列表obejct存储到view bag并在视图中设置为Dropdownlist。
ViewBag.Location=lstLocation ;
@Html.DropDownList("Location", ViewBag.Location as List<SelectListItem>,
"-SELECT-", new { data_val = "true", data_val_required = "Please select
location", @class = "form-control" })
使用上面的代码,您的数据将在下拉列表中显示为“1-ROnak”,“2-Manish”。 选择后,您可以使用“ - ”分割拆分名称和ID。
谢谢。
答案 1 :(得分:0)
在控制器中:
ViewBag.EmployeeDetails= db.EmployeeDetails.ToList();
在视图中:
@model ...
@{
ViewBag.Title ="Employe ";
List<Proyect.Models.EmployeeDetails> Details = ViewBag.EmployeeDetails;
}
<select name="EmployeeDetails" class="form-control" id="EmployeeDetails">
<option>Select one</option>
@foreach (var Detail in Details)
{
<option id="@Docs.Id_Employee_Detail" value="@Docs.Id_Employee_Detail">@Detail.Name : @Detail.Department</option>
}
</select>
并在控制器中收到参数
public ActionResult Something(int EmployeeDetails)
{
var EmployeeDetailSelected = dc.tbl_EmployeeDetails.Find(EmployeeDetails);
}