簡體   English   中英

HTML.EditorFor拋出屬性未找到錯誤

[英]Html.EditorFor throws property not found error

我從已經難倒我的MVC3 Web表單視圖得到一個奇怪的錯誤。

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

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。

但是,如果我注釋掉此語句,則我在VIN上的EditorFor繼續正常運行:

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

有什么我想念的嗎?

這聽起來像EditorFor的限制。 可能不夠聰明,無法檢查實現的接口的屬性。

找到解決方案:

我在IAutomobile界面上重新實現了“名稱”屬性:

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

我沒有對我的具體類進行任何更改,並且EditorFor方法可以使用Name屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM