類似のタイトルを見ていますが、私はなぜエラーが発生するのか理解しています。私はEditorForModel
を使用するように修正する方法を理解しようとしています。私はこのエラーを取得しています:エディタテンプレート(ラジオボタンリスト)と辞書に渡されたモデルアイテムは[enum]タイプですが、辞書にはモデルタイプが必要ですX
The model item passed into the dictionary is of type 'MyNameSpace.ViewModels.MyData+MyEnum', but this dictionary requires a model item of type 'MyNameSpace.ViewModels.MyData'.
マイモデル:
[UIHint("MyRadioButton")]
public MyEnum MyRadioRadioButton { get; set; }
//
//
public enum MyEnum
{
Choice1,
Choice2
}
私はMyRadioButton.cshtml
と呼ばれるEditorTemplateを呼び出すこと[UIHint]
を使用しています。さて、私の見解では、@Html.EditorForModel
を使ってEditorTemplateを呼び出しています。
@Html.EditorForModel("BasicDetails")
両方のテンプレートが「/共有/ EditorTemplates /」フォルダにあります。これは、一般的なテンプレートを呼び出しているビューページの一部です。
これはMyRadioButton.cshtml
テンプレートです:
<td>
<div class="radio">
@Html.RadioButtonFor(m => m.MyRadioButton, "Choice1")<br />
@Html.RadioButtonFor(m => m.MyRadioButton, "Choice2")
</div>
</td>
これは(上記@Html.EditorForModel
によって呼び出される)BasicDetails.cshtml
次のとおりです。
@using MyNameSpace.ViewModels
@model MyData
<table>
@Html.EditorFor(x => x.FirstName)
@Html.EditorFor(x => x.LastName)
@Html.EditorFor(x => x.MyRadioButton) //This is where my error is thrown
</table>
があるので、私は上記のRadioButtonListのエディタテンプレートで複雑な何かを避けたいです他のものはそこに進んでいます(私は余分なものをすべて取り除き、まだエラーを受けています)。私は特定のradiobuttonlistをさまざまなビューで複数回使用しています(これがコピー/ペーストではなくテンプレートにしたい理由です)。何かアドバイス?
これは機能しません。コードをテンプレートにコピー/ペーストするだけです。 – REMESQ