2011-09-16 51 views
10

我似乎無法弄清楚爲什麼這樣做不起作用。我使用ASP.NET MVC2,我只是試圖通過將該代碼放到/Shared/EditorTemplates/String.ascx覆蓋默認編輯器外觀:EditorFor - 傳入字典的模型項目類型爲'System.Int32',但是此字典需要一個'System.String'類型的模型項目。

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %> 
<%=Html.TextBox(null, Model, new { @class="Text" }) %> 

然後在我的視圖頁,我有這個線是Int32類型的:

<%: Html.EditorFor(model => model.AppID) %> 

出於某種原因,這導致錯誤:

System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'System.String'. 

我看不到任何東西如何能是錯我的結束,這是很簡單的。如果類型是Int32,爲什麼它會嘗試將編輯器用於字符串?我還應該提及,我已經覆蓋編輯器的布爾?鍵入(以呈現布爾值作爲複選框),並且它在同一頁面上工作得很好。

編輯

嗯,我搜索了很多次,但我沒有看到這篇文章,直到我發現它在「相關」鏈接。我想這會的工作,我仍然認爲這是一個混亂和不一致的實現,但:

Asp.net Mvc Display template of String, but now every simple type wants to use it!

回答

1

在你的編輯模板你告訴它期待ViewUserControl<string>但你傳遞一個intEditorFor

由於編輯器模板正在等待string,並且您傳遞的是int,所以無法工作。

+0

好吧,好吧,我想這是令我感到困惑的部分。我認爲它使用反射來選擇正確的編輯器模板。情況並非如此嗎?就像我說的,我有布爾?模板,並正確呈現布爾值 - 它不會調用字符串模板。 – user949286

+0

你有單獨的int模板嗎? –

+0

不,我不知道。我不認爲我會需要一個,因爲我第一次創建了布爾?模板,一切都繼續有效。然後我創建了字符串模板,併發生此錯誤。也許我應該只是創建int模板,但我可能有很多其他類型,我希望它使用默認模板,因此我不必生成不必要的冗餘模板。 – user949286

1

我剛遇到這個錯誤,但帶有DateTime。我可以通過更改編輯器模板來使用Object作爲其模型類型,從而使事情再次滾動。

劍道UI電網
5

做:

public class BookBean 
    { 
     [ScaffoldColumn(false)] 
     public Int32 Id { set; get; } 

     public String Title { set; get; } 

     public String Author { set; get; } 

     public String Publisher { set; get; } 

     [UIHint("Integer")] 
     public Int32 Price { set; get; } 

     [UIHint("Integer")] 
     public Int32 Instore { set; get; } 

     [UIHint("Integer")] 
     public Int32 GroupId { get; set; } 
    } 
在Integer.ascx共享/ EditorTemplate文件夾

做:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int?>" %> 

<%: Html.Kendo().IntegerTextBoxFor(m => m) 
     .HtmlAttributes(new { style = "width:100%" }) 
     .Min(int.MinValue) 
     .Max(int.MaxValue) 
%> 
0

我面臨的Int16場同樣的問題,雖然我在整型模板項目,所以我不得不創建一個特定的模板Int16 FileName:Int16.cshtml Html:

@model Int16? 

@Html.TextBoxFor(model => model) 
相關問題