不受模型MVC3约束力,但发现继承财产的接口工作在MVC2罚款 [英] Inherited interface property not found by Model Binding in MVC3 but works fine in MVC2

查看:114
本文介绍了不受模型MVC3约束力,但发现继承财产的接口工作在MVC2罚款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:问题就迎刃而解了

达林季米特洛夫的回答包含一个链接到另一个相关的问题。 其中提出的解决方案在该网页上的结束了为我们工作。


原始的问题

在code样品低于MVC 2的作品,但罚全中,MVC 3,异常有谁知道为什么MVC 3介绍了重大更改?有没有办法得到这个工作在MVC 3,同时还允许我从视图中描述视图模型作为一个接口?

有是另一个<一个href=\"http://stackoverflow.com/questions/7968304/inherited-interface-property-not-found-by-model-binding\">related 的问题在计算器上,但它并没有给我任何信息,为什么有MVC 2和MVC 3之间的差异。

型号

 公共接口IPerson {
    字符串名称{;组; }
}公共接口ISPY:IPerson {
    字符串$ C $ {CNAME获得;组; }
}公共类间谍:ISPY {
    公共字符串名称{;组; }
    公共字符串$ C $ {CNAME获得;组; }
}

控制器动作

 公众的ActionResult指数(){
    VAR模型=新的间谍{名称=詹姆斯·邦德,codeNAME =007};
    返回查看(模型);
}

MVC2视图(完美的作品)

 &LT;%@页面语言=C#继承=System.Web.Mvc.ViewPage&LT; MvcApp.Models.ISpy&gt;中%GT;
&LT; P&GT; codeNAME:LT;%:Html.TextBoxFor(X =&GT; X codeNAME)%GT;&LT; / P&GT;
&所述p为H.;名称:其中;%:Html.TextBoxFor(X =&GT; x.Name)%GT;&下; / P&GT;

MVC3查看(抛出异常)

  @model MvcApp.Models.ISpy
&LT; P&GT;名称:@ Html.TextBoxFor(X =&GT; x.Name)LT; / P&GT;
&LT; P&GT; codeNAME:@ Html.TextBoxFor(X =&GT; X codeNAME)&LT; / P&GT;


MVC3异常信息

我显示相关的异常信息下方,但你可以在这里查看错误页面的整个输出: HTTPS://gist.github .COM / 1443750

 运行时异常
System.ArgumentException:属性'** ** MvcApp.Models.ISpy.Name`找不到。源错误:1号线:@model MvcApp.Models.ISpy
2号线:其中,P&​​GT; codeNAME:@ Html.TextBoxFor(X =&GT; X codeNAME)&LT; / P&GT;
3号线:​​其中,P&​​GT;名称:@ Html.TextBoxFor(X =&GT; x.Name)LT; / P&GT;
堆栈跟踪:[ArgumentException的:酒店MvcApp.Models.ISpy.Name找不到。]
   System.Web.Mvc.AssociatedMetadataProvider.GetMetadataForProperty(Func`1 modelAccessor,类型containerType,弦乐propertyName的)502169
   System.Web.Mvc.ModelMetadata.GetMetadataFromProvider(Func`1 modelAccessor,类型modelType,字符串propertyName的,类型containerType)+101
   System.Web.Mvc.ModelMetadata.FromLambdaEx pression(前pression`1前pression,ViewDataDictionary`1可视数据)+421
   System.Web.Mvc.Html.InputExtensions.TextBoxFor(HtmlHelper`1的HtmlHelper,防爆pression`1前pression,IDictionary`2 htmlAttributes)+58
   System.Web.Mvc.Html.InputExtensions.TextBoxFor(HtmlHelper`1的HtmlHelper,防爆pression`1前pression)+50
   ASP._Page_Views_Home_Index_cshtml.Execute()在C:\\项目\\ MvcApplication1 \\ MvcApplication1 \\查看\\首页\\ Index.cshtml:3


  

帮助我们达林季米特洛夫,你是我们唯一的希望。



解决方案

我已经<一个href=\"http://connect.microsoft.com/VisualStudio/feedback/details/636341/modelmetadata-fromlambdaex$p$pssion-has-changed-in-asp-net-mvc-3-rtm\">brought这个问题,以微软的注意。不幸的是我得到的回应是:对不起,这是由设计。我个人从这样的答案得出的结论是,MVC开发团队的正在设计的,如果这是由设计马车产品。他们不是被指责采取这样的设计决策,他们是责怪不断裂的任何地方提到它改变了发行说明文档的部分。如果他们提到它,那么它会一直是我们的错,当我们采取的决定,即使我们特意知道关于重大更改迁移。

总之,这里是一个相关问题

下面是从MVC开发团队的回答是:


  

不幸的是,code实际上是利用被修正了一个错误,
  其中,一个前pression为ModelMetadata目的的容器
  不经意间设置为声明类型,而不是含
  类型。此bug已被固定,由于虚拟需求
  性能和验证/模型元。


  
  

有基于接口的机型是不是我们鼓励的东西(也没有,
  鉴于该bug修复本身的限制,实际上能
  支持)。切换到抽象基类会解决这个问题。


UPDATE: Problem Solved.

Darin Dimitrov's answer contains a link to another, related question. One of the proposed solutions on that page ended up working for us.


Original Question

The code sample below works in MVC 2 but throws an exception in MVC 3. Does anyone know why MVC 3 introduced this breaking change? Is there a way to get this working in MVC 3 while still allowing me to describe the viewmodel as an Interface from within the view?

There is another related question on StackOverflow, but it doesn't give me any info as to why there is a difference between MVC 2 and MVC 3.

Model

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 Action

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

MVC2 View (Works perfectly)

<%@ 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 (Throws exception)

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


MVC3 Exception Information

I am showing the relevant exception info below, but you can view the entire output of the error page here: 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.

解决方案

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.

这篇关于不受模型MVC3约束力,但发现继承财产的接口工作在MVC2罚款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆