gpt4 book ai didi

asp.net-mvc - MVC3 中的模型绑定(bind)未找到继承的接口(interface)属性,但在 MVC2 中工作正常

转载 作者:行者123 更新时间:2023-12-02 22:57:02 24 4
gpt4 key购买 nike

更新:问题已解决。

Darin Dimitrov 的回答包含另一个相关问题的链接。 One of the proposed solutions该页面上的内容最终为我们工作。

<小时/>

原始问题

下面的代码示例在 MVC 2 中工作,但在 MVC 3 中抛出异常。有谁知道为什么 MVC 3 引入了这一重大更改?有没有办法让它在 MVC 3 中工作,同时仍然允许我从 View 内将 View 模型描述为接口(interface)?

还有一个related question在 StackOverflow 上,但它没有给我任何关于为什么 MVC 2 和 MVC 3 之间存在差异的信息。

型号

public interface IPerson {
string Name { get; set; }
}

public interface ISpy : IPerson {
string CodeName { get; set; }
}

public class Spy : ISpy {
public string Name { get; set; }
public string CodeName { get; set; }
}

Controller 操作

public ActionResult Index() {
var model = new Spy { Name = "James Bond", CodeName = "007" };
return View(model);
}

MVC2 View(完美运行)

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApp.Models.ISpy>" %>
<p>CodeName: <%: Html.TextBoxFor(x => x.CodeName) %></p>
<p>Name: <%: Html.TextBoxFor(x => x.Name) %></p>

MVC3 View (抛出异常)

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

MVC3异常信息

我在下面显示了相关的异常信息,但您可以在此处查看错误页面的完整输出:https://gist.github.com/1443750 .

Runtime Exception
System.ArgumentException: The property **`MvcApp.Models.ISpy.Name`** could not be found.

Source Error:

Line 1: @model MvcApp.Models.ISpy
Line 2: <p>CodeName: @Html.TextBoxFor(x => x.CodeName)</p>
Line 3: <p>Name: @Html.TextBoxFor(x => x.Name)</p>


Stack Trace:

[ArgumentException: The property MvcApp.Models.ISpy.Name could not be found.]
System.Web.Mvc.AssociatedMetadataProvider.GetMetadataForProperty(Func`1 modelAccessor, Type containerType, String propertyName) +502169
System.Web.Mvc.ModelMetadata.GetMetadataFromProvider(Func`1 modelAccessor, Type modelType, String propertyName, Type containerType) +101
System.Web.Mvc.ModelMetadata.FromLambdaExpression(Expression`1 expression, ViewDataDictionary`1 viewData) +421
System.Web.Mvc.Html.InputExtensions.TextBoxFor(HtmlHelper`1 htmlHelper, Expression`1 expression, IDictionary`2 htmlAttributes) +58
System.Web.Mvc.Html.InputExtensions.TextBoxFor(HtmlHelper`1 htmlHelper, Expression`1 expression) +50
ASP._Page_Views_Home_Index_cshtml.Execute() in c:\Projects\MvcApplication1\MvcApplication1\Views\Home\Index.cshtml:3

Help us Darin Dimitrov, you're our only hope.

最佳答案

我已经brought this issue引起微软的注意。不幸的是,我得到的答复是:抱歉,这是设计使然。就我个人而言,我从这样的答案中得出的结论是,MVC 团队正在设计一个有缺陷的产品(如果这是有意设计的话)。他们不应该因为做出这样的设计决定而受到责备,而应该责怪他们没有在发行说明文档的重大更改部分中提及它。如果他们提到了这一点,那么即使我们故意知道这一重大变化,当我们做出迁移决定时也会是我们的错。

无论如何,这是一个 related question

这是 MVC 团队的回答:

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.

关于asp.net-mvc - MVC3 中的模型绑定(bind)未找到继承的接口(interface)属性,但在 MVC2 中工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8448917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com