2016-06-13 9 views
-1

データベースから選択した溺れたドロップ値を取得するにはどうすればよいですか?私は選択された値(整数)をドロップダウンして値とテキストを他のテーブルドロップダウンで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> 

私は完全なリストと示すドロップダウンをフェッチ示していたコードです。 ここで私はドロップダウンリストを取得しています。

Selected = x.dropdownName == dropdownname 

しかし、それは本当に:しかし、私はあなたがSelectListItemに選択しているとき

+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

は、単に追加することを行う方法

(格納された値が選択してください)選択したテキストで一覧を取得したいです選択リストをバインドできる値がModelStateである限り、必須ではありません。私はあなたのビューで何をしているのかよく分かりませんが、Html.DropDownListの最初のパラメータは、投稿のそのプロパティにバインドするために必要なモデルのプロパティに対応する必要があります。プロパティと一致する場合、Razorはそのプロパティの値を使用して選択リストに適切な選択値を自動的に設定します。

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

その方法は、あなたが何かリアルに結合している式で知っているのではなく、ちょうどあなたの文字列名が正しいことを望ん:ヘルパーの*Forファミリを使用した場合、それは価値がある何のために、これははるかに簡単です縛り付けます。

関連する問題