2012-02-24 77 views
0

查看類似的標題,但沒有任何內容在點上或沒有任何工作。我明白爲什麼會出現錯誤,我只是想弄清楚如何使用我的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    
    } 

我使用[UIHint]調用了一個名爲EditorTemplate MyRadioButton.cshtml。現在,我的觀點還使用@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> 

這是BasicDetails.cshtml(由上述@Html.EditorForModel叫):

@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> 

我想避免在上面的單選按鈕列表編輯模板任何複雜的,因爲有其他東西在那裏(我剝去了一切多餘的東西,我仍然得到錯誤)。我在不同的視圖中多次使用特定的單選按鈕列表(這就是爲什麼我想模板而不是複製/粘貼)。有什麼建議?

回答

0

現在我只是依靠EditorForModel/EditorTemplates/中提取模板,而不是使用[UIHint]作爲單選按鈕列表,我只是將0123該模板中的組。這對我有用,並可以最大限度地減少複製/粘貼。

在某些時候,我必須學習停止使用模板>模板>模板範例,並知道何時足夠了。 :)

0

從BasicDetails.cshtml您調用EditorFor爲@Html.EditorFor(x => x.MyRadioButton)

這意味着傳遞給EditorFor的模型類型是Enum類型。

但是在EditorFor Template(MyRadioButton.cshtml)中,我認爲你已經使用這個類作爲模型。所以錯誤。

因此,我們需要在MyRadioButton.cshtml改變模型類型MyEnum(命名空間)

通過同型號editorFor模板@Html.EditorFor(x=>x,"MyRadioButton")

+0

這不起作用。只要將代碼複製/粘貼到模板中即可。 – REMESQ 2012-02-25 14:59:41