データベース内のインデックスに対するプロパティの説明のマッピングである列挙型があります。その列挙型のインスタンスを表す ViewModel のプロパティがあります。列挙型インスタンスのリストを返す両方を試しました。つまり、これを行います。
@Html.DropDownListFor(m => m.CurrentFilter,
Model.FilterTypes.Select(entry =>
new SelectListItem{ Text = entry.ToString(), Value = ((int)entry).ToString()}),
new { @class = "normalcell", style = "WIDTH: 132px;" })
SelectListItems のリストを返します。つまり、次のようにします。
@Html.DropDownListFor(m => m.CurrentFilter,
Model.FilterTypes.Select(entry =>
new SelectListItem{ Text = entry.Text, Value = entry.Value, Selected = entry.Selected}),
new { @class = "normalcell", style = "WIDTH: 132px;" })
2 番目のケースでは、デバッグ時に、エントリ オブジェクトの Selected プロパティが正しい項目に対して true であることを確信しています。どちらの場合も、HTML に「selected」属性が書き込まれていないため、正しいアイテムが選択されていません。ブレークポイントも設定しました。CurrentFilter DOES には正しい値があり、残りのページは適切にレンダリングされるため、値を見つけています。
同様のコードを使用して、機能するドロップリストをたくさん書いてきました。
私も試しました:
@Html.DropDownListFor(m => m.CurrentFilter,
Model.FilterTypes,
new { @class = "normalcell", style = "WIDTH: 132px;" })
これは論理的な方法のように思えます (SelectListItems のリストを返し、ページで処理を行わないだけです) が、Selected プロパティは依然として無視されます。
アップデート:
私はこのようにしようとしました:
@Html.DropDownList("CurrentFilter", Model.FilterTypes, new { @class = "normalcell", style = "WIDTH: 132px;" })
リクエストから値を読み取るだけです。Selected == true であるアイテムが 1 つだけのリストを返しているのは今でもそうであり、MVC がそれを無視しているのもまだです。
これは驚くことではありませんが、他のすべてがうまくいかない理由を知りたいです。
<select class="normalcell" id="CurrentFilter" name="CurrentFilter" style="WIDTH: 132px;">
@foreach (SelectListItem item in Model.FilterTypes)
{
if (item.Selected)
{
<option value="@item.Value" selected="selected">@item.Text</option>
}
else
{
<option value="@item.Value">@item.Text</option>
}
}