-1
如何從數據庫中獲取選定的淹沒下降值?我將員工詳細信息存儲在一個表格中,其下拉選擇值(整數)和其他表格下拉值和文本。添加下拉列表與數據庫中的選定值
我的模型是
public class EmployeeDetails
{
public int empid {set;get;}
public string empname{set;get;}
public string empdesig{set;get;}
public decimal empsal{set;get;}
public int education{set;get;}
public DateTime time{set;get;}
public string dropdownname { set; get; }
}
public class DropDownList
{
public int dropdownId { set; get; }
public string dropdownName { set; get; }
}
public class DisplayEmployeeViewModel
{
public IEnumerable<EmployeeDetails> EmployeeDetails { set; get; }
public DisplayCreateEmployeeViewModel createemployee { set; get; }
public string TabName { set; get; }
public List<SelectListItem> DropdownSelectListItems
{
get
{
var dropdown = (new DropdownRepository()).GetDropdownTypes();
var entityList = dropdown.Select(x => new SelectListItem()
{
Text = x.dropdownName,
Value = x.dropdownId.ToString()
});
return entityList.ToList();
}
}
}
public class DisplayCreateEmployeeViewModel
{
public EmployeeDetails EmployeeDetails { set; get; }
public int empid { set; get; }
public string empname { set; get; }
public string empdesig { set; get; }
public decimal empsal { set; get; }
public int education { set; get; }
public DateTime time { set; get; }
public string dropdownname { set; get; }
public List<SelectListItem> DropdownSelectListItems
{
get
{
var dropdown = (new DropdownRepository()).GetDropdownTypes();
var entityList = dropdown.Select(x => new SelectListItem()
{
Text = x.dropdownName,
Value = x.dropdownId.ToString()
});
return entityList.ToList();
}
}
}
查看
<div>
<table id="tblemployeedetails" class="table table-hover table-bordered table-responsive " >
<thead style="background-color:black;color:white; ">
<tr>
<th></th>
<th>Employee Name</th>
<th>Employee Designation</th>
<th>Enployee Education</th>
<th>Employee Salary</th>
</tr>
</thead>
<tbody>
@foreach (EmployeeDetails details in Model.EmployeeDetails)
{
<tr>
<td>@Html.CheckBox("status" + details.empid, new {value=details.empid,@class="detailCheckBox" })</td>
<td>@Html.TextBox(details.empname, details.empname, new {@class="txtdisable" })</td>
<td>@Html.TextBox(details.empdesig, details.empdesig, new { @class = "txtdisable" })</td>
<td>@Html.DropDownList(details.education.ToString(),Model.DropdownSelectListItems, new { @class = "txtdisable" })</td>
<td>@Html.TextBox(details.empsal.ToString(), details.empsal, new { @class = "txtdisable" })</td>
</tr>
}
</tbody>
</table>
</div>
其中我展示取下拉完整列表和顯示代碼。 這裏我得到了下拉列表。但是,我想與選定的文本列表(存儲的值應選擇)
如何做到這一點
首先提到[這個答案](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) - 你不能使用一個'foreach'循環綁定到一個集合。然後,一旦你糾正了這一點,請參考[這個答案](http://stackoverflow.com/questions/37407811/mvc5-razor-html-dropdownlistfor-set-selected-when-value-is-in-array/37411482#37411482 ) –