Inherited interface property not found by Model Binding in MVC3 but works fine in MVC2

天涯浪子 提交于 2019-12-04 02:27:10
Darin Dimitrov

I have already brought this issue to the attention of Microsoft. Unfortunately the response I got was: sorry that's by design. Personally the conclusion I draw from such an answer is that the MVC team is designing a buggy product if that's by design. And they are not to be blamed for taking such a design decision, they are for blaming for not mentioning it anywhere in the breaking changes section of the release notes document. Had they mentioned it, then it would have been our fault when we took the decision for migrating even if we deliberately knew about the breaking change.

Anyway, here's a related question.

And here's an answer from the MVC team:

Unfortunately, the code was actually exploiting a bug that was fixed, where the container of an expression for ModelMetadata purposes was inadvertently set to the declaring type instead of the containing type. This bug had to be fixed because of the needs of virtual properties and validation/model metadata.

Having interface-based models is not something we encourage (nor, given the limitations imposed by the bug fix, can realistically support). Switching to abstract base classes would fix the issue.

Change this line:

@model MvcApp.Models.ISpy

To

@model MvcApp.Models.Spy

I found a pathetic workaround which allows you to use this model structure:

@model MvcApp.Models.IPerson
<p>Name: @Html.TextBoxFor(x => x.Name)</p>
@Html.Partial('_SpyBox')

And in _SpyBox.cshtml

@model MvcApp.Models.ISpy
<p>CodeName: @Html.TextBoxFor(x => x.CodeName)</p>

Meh.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!