简体   繁体   中英

Html.EditorFor throws property not found error

I am getting an odd error from an MVC3 webforms view that has stumped me.

Setup: I have two interfaces defined:

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; }
}

My view is a strongly-typed view: System.Web.Mvc.ViewPage<IAutomobile> and my controller

When I attempt to use a EditorFor on the Name property

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

I get an exception: System.ArgumentException: The property IAutomobile.Name could not be found.

However, if I comment out this statement, my EditorFor on the VIN continues to function properly:

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

Is there something I'm missing?

This sounds like a limitation of EditorFor . Perhaps it's not smart enough to check implemented interfaces for properties.

Solution found:

I re-implemented the 'Name' property on the IAutomobile interface:

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

I made no changes to my concrete class, and the EditorFor method was able to work with the Name property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM