Root interface property not accessible via parent

2019-04-08 20:14发布

问题:

I feel like I am missing something basic here.

I have 2 interfaces. One is called IIdentifiable which specifies the existence of an Id property. The second can really be anything that is also identifiable. See code below.

public interface IIdentifiable
{
    Guid Id { get; set; }
}
public interface IPerson : IIdentifiable
{
    string Name { get; set; }
}

The problem:

When using the Html.HiddenFor (or any other helper method) and the model is strongly typed to the IPerson interface, I get an error when accessing the properties of the IIdentifiable interface. I can access and display the data in the IIdentifiable properties by going directly against Model.Id but not when using the lambda expression and accessing the model that way.

The error I get is "The property *.IPerson.Id could not be found".

This only seems to be the case when using the Razor view engine. I know this was working using ASP.NET MVC 2 and the WebForms view engine. I haven't tried it on the ASP.NET MVC 3 using the WebForms view engine yet.

回答1:

Yes, I have brought this to the attention of Microsoft by opening a ticket. Also see this related question.



回答2:

I always feel less insane when other people have the same problems I do. As someone said in the link Darin provided, this is a work around or now:

<%: Html.HiddenFor(m => (m as IIdentifiable).Id) %>