2011-03-05 46 views
0

我從MVC3 webforms視圖中看到一個奇怪的錯誤,它讓我難堪。Html.EditorFor throws屬性找不到錯誤

設置: 我有兩個接口定義:

public interface IDbObject { 
    int Id { get; set; } 
    string Name { get; set; } 
} 

public interface IAutomobile : IDbObject { 
    string VIN { get; set; } 
} 

public class Automobile : IAutomobile { 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string VIN { get; set; } 
} 

我的觀點是一個強類型的視圖:System.Web.Mvc.ViewPage<IAutomobile>和我的控制器

當我嘗試在Name屬性使用EditorFor

<%= Html.EditorFor(a => a.Name) %> 

我收到一個異常:System.ArgumentException:屬性IAutomobile.Name無法b e找到了。

但是,如果我註釋掉這個說法,我對VIN EditorFor繼續正常運行:

<%= Html.EditorFor(a => a.VIN) %> 

有我丟失的東西?

回答

0

發現的解決方案:

我重新實現IAutomobile界面上的「名稱」屬性:

public interface IAutomobile : IDbObject { 
    new string Name { get; set; } 
    string VIN { get; set; } 
} 

我做任何更改我的具體類和EditorFor方法能夠一起工作Name屬性。

+0

我有一種感覺,這可能會導致在使用對象作爲IDbObject時出現問題。 – 2011-03-05 15:51:11

+0

你可能是對的......我會讓你知道;) – 2011-03-05 15:54:20

1

這聽起來像是EditorFor的限制。也許它不夠聰明來檢查實現的接口的屬性。

+0

它看起來好像沒有檢查實現的接口。我想我找到了我的解決方案 – 2011-03-05 15:47:01

+0

實際上,這個錯誤不是來自EditorFor,而是來自AssociatedMetadataProvider – Linkgoron 2011-03-05 15:58:28

+0

嗯,我實際上認爲這可能會計算AssociatedMetadataTypeTypeDescriptionProvider類的錯誤。 – Linkgoron 2011-03-05 16:12:00

相關問題