2016-06-13 77 views
-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> 

其中我展示取下拉完整列表和顯示代碼。 這裏我得到了下拉列表。但是,我想與選定的文本列表(存儲的值應選擇)

如何做到這一點

+0

首先提到[這個答案](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 ) –

回答

0

當你選擇到SelectListItem,只需添加:

Selected = x.dropdownName == dropdownname 

然而,真正只要在ModelState中有一個值可以與選擇列表綁定即可。我不確定你在視圖中究竟做了什麼,但Html.DropDownList的第一個參數應該與模型上的屬性相對應,因爲它需要在後期綁定回該屬性。如果它與某個屬性匹配,則Razor將使用該屬性的值自動在選擇列表中設置適當的選定值。

@Html.DropDownListFor(m => m.SomeProperty, Model.SomePropertyChoices) 

這樣,你知道的,你要綁定的東西真正的表達,而不是隻是希望您的字符串名稱是正確的:對於它的價值,使用*For家庭傭工時,這是很容易並會綁定。