2011-11-24 14 views
1

私のプロジェクトでは、DropDownListFor(x => xコードはEditorTemplateに置かれています)は、テーブルのフィールドの1つをドロップダウンリストにしています。 。ドロップダウンリストから、私は見ていないよ私はViewModelにに設定をしています事前に選択した項目にデフォルト設定はありませんDropDownListFor()はViewModelからあらかじめ選択された項目を設定しませんか?

コードを次のように?

のViewModel:

public class FooDetailViewModel : ViewModelBase 
{ 
    public List<FooPermissionObject> FooPermissions { get; set; } 
} 

厳密に型指定されたモデルオブジェクト:

public class FooPermissionObject 
{ 
    public string Name { get; set; } 
    public int Reason { get; set; } 
    public IEnumerable<SelectListItem> Reasons { get; set; } 
    public bool Selected { get; set; } 
} 

コントローラ:

var viewModel = new StockLineManagementDetailViewModel(); 

using (_model) 
{ 
    foreach (var company in _model.GetAllRecords<Company>()) 
    { 
     var permissionModel = new FooPermissionObject 
     { 
      Name = company.Name, 
      Selected = true, 
      Reasons = _model.GetAllRecords<FooPermissionReason>() 
        .ToList() 
        .Select(x => new SelectListItem 
        { 
         Value = x.FooPermissionReasonId.ToString(), 
         Text = x.FooPermissionReasonDesc 
        }), 
      Reason = record.FooPermissionReasonId 
     }; 

     viewModel.FooPermissions.Add(permissionModel); 
    } 
} 

ビュー:

<table id="myTable" class="tablesorter" style="width:98%"> 
    <thead> 
     <tr> 
     <th> 
      Name 
     </th> 
     <th> 
      Excluded 
     </th> 
     <th> 
      Reason for Exclusion 
     </th> 
     </tr> 
    </thead> 
    <tbody> 
     @Html.EditorFor(x => x.FooPermissions) 
    </tbody> 
</table> 

EditorTemplate:これは表されるオブジェクトでDropDownListForを移入しませんなぜ

@model FooPermissionObject 
<tr> 
    <td> 
     @Html.DisplayFor(x => x.Name, new { @readonly = "readonly"}) 
     @Html.HiddenFor(x => x.Name) 
    </td> 
    <td> 
     @Html.CheckBoxFor(x => x.Selected) 
    </td> 
    <td> 
     @Html.DropDownListFor(x => x.Reason, Model.Reasons) 
    </td> 
</tr> 

誰もが任意のアイデアを得ました理由コレクションによって理由値によって

答えて

1

私はあなたの選択リストに= trueを選択した設定されている任意のコードを参照することはできません。 FooPermissionObjectのSelectedプロパティを設定していますが、ドロップダウンリストが理由コレクションにバインドされていません。

.Select(x => new SelectListItem 
{ 
    Value = x.FooPermissionReasonId.ToString(), 
    Text = x.FooPermissionReasonDesc, 
    Selected = (Some codition or other) 
}) 

は何でもあなたの基準にいくつかの条件や他の交換に選択されるべき項目言うことです:あなたはこのような何かをしたいです。

EDIT:

良い方法は以下の通りであるかもしれない:

Reasons = new SelectList(_model.GetAllRecords<FooPermissionReason>(), 
         "FooPermissionReasonId", 
         "FooPermissionReasonDesc", 
         record.FooPermissionReasonId) 

SelectListののコンストラクタへのparamsは、以下のとおりです。バインドするコレクション、値フィールド、テキストフィールド、選択した値。

+0

私が知っていた限り(これは間違っているかもしれません)設定Html.DropDownListFor(x => x.Reason、Model.Reasons )Selected項目をReasonフィールドとして自動的にバインドしますか? – M05Pr1mty

+0

はいユーザーが選択する値は、reasonプロパティのモデルで設定された値ですが、アイテムを事前に選択することとは関係ありません。 –

+0

ああ、本当ですか?だから私はまだ唯一のユーザーが何かを選択したときにバインドされるフィールドを設定しているので、ダッシュドットのwould'ntのアドバイスに従っても? – M05Pr1mty

0

編集 Selectedプロパティを設定すると、別の方法(今や一方向)がSelectListItemに表示されます(必要に応じて)。このトピックに関する

ニースの記事が見つけることができます:http://codeclimber.net.nz/archive/2009/08/10/how-to-create-a-dropdownlist-with-asp.net-mvc.aspx

+0

投影されたSelectListItemについての興味深い点。試してみてください。 – M05Pr1mty

+0

あなたにさらなる助けが必要ならば教えてください – dasheddot

+0

コードの最初のビットはコンパイルされませんReasonはintで、IEnumerable に設定することはできません。 –

関連する問題