有人可以帮我澄清一下吗?在我的 ASP.NET MVC 2 应用程序中,我有一个 BaseViewModel
类,其中包含以下方法:
public virtual IDictionary<string, object> GetHtmlAttributes<TModel, TProperty>
(Expression<Func<TModel, TProperty>> propertyExpression)
{
return new Dictionary<string, object>();
}
其想法是每个子视图模型都可以重写此方法并提供一组合适的 html 属性,基于在某些逻辑上,要在视图中呈现:
<%: Html.TextBoxFor(model => model.MyProperty, Model.GetHtmlAttributes
(model => model.MyProperty)) %>
但是,当按照上面的行使用时,当我点击视图时会出现编译错误:
方法 '...BaseViewModel.GetHtmlAttributes 的类型参数无法从用法推断表达式)
'。尝试显式指定类型参数。
我必须执行以下操作:
<%: Html.TextBoxFor(model => model.MyProperty, Model.GetHtmlAttributes
<ChildModel, string>(model => model.MyProperty)) %>
我只是想弄清楚它如何尝试推断类型,在 HtmlHelper/TextBoxFor
扩展方法中这样做没有问题吗?
是否因为视图中的 HtmlHelper
会自动采用与页面顶部的 ViewUserControl
中指定的类型相同的类型,而我的代码可以用于继承的任何类型来自BaseViewModel
?是否可以以可以推断我的模型/属性类型的方式编写此代码?
Could someone please clarify something for me. In my ASP.NET MVC 2 app, I've got a BaseViewModel
class which includes the following method:
public virtual IDictionary<string, object> GetHtmlAttributes<TModel, TProperty>
(Expression<Func<TModel, TProperty>> propertyExpression)
{
return new Dictionary<string, object>();
}
The idea being that each child viewmodel can override this method and provide a suitable set of html attributes, based on some logic, to be rendered in the view:
<%: Html.TextBoxFor(model => model.MyProperty, Model.GetHtmlAttributes
(model => model.MyProperty)) %>
However when used as in the line above, I get a compilation error when I hit the view:
The type arguments for method '...BaseViewModel.GetHtmlAttributes<TModel,TProperty> Expression<System.Func<TModel,TProperty>)
' cannot be inferred from the usage. Try specifying the type arguments explicitly.
I have to do the following:
<%: Html.TextBoxFor(model => model.MyProperty, Model.GetHtmlAttributes
<ChildModel, string>(model => model.MyProperty)) %>
I'm just looking for some clarity as to how it tries to infer the type, it has no problem doing so in the HtmlHelper/TextBoxFor
extension method?
Is it because HtmlHelper
in the view will automatically be for the same type as is specified in the ViewUserControl
at the top of the page, whereas my code can be for any type inheriting from BaseViewModel
? Is is possible to write this in such a way that it can infer my model/property types?
发布评论
评论(9)
我知道这个问题已经有一个公认的答案,但对于我这个 .NET 初学者来说,有一个简单的解决方案可以解决我做错的事情,我想我会分享。
我一直在这样做:
对我有用的是更改为:(
其中“m”是表示模型对象的任意字符串)
I know this question already has an accepted answer, but for me, a .NET beginner, there was a simple solution to what I was doing wrong and I thought I'd share.
I had been doing this:
What worked for me was changing to this:
(where "m" is an arbitrary string to represent the model object)
在您的示例中,编译器无法知道
TModel
应该是什么类型。您可以做一些与您可能尝试使用扩展方法做的事情接近的事情。但我认为你不可能拥有类似于虚拟的东西。
编辑:
实际上,您可以使用自引用泛型进行
虚拟
:In your example, the compiler has no way of knowing what type should
TModel
be. You could do something close to what you are probably trying to do with an extension method.But you wouldn't be able to have anything similar to
virtual
, I think.EDIT:
Actually, you can do
virtual
, using self-referential generics:我也遇到了同样的问题,我的解决方案:
在 web.config 文件中:
I had this same problem, my solution:
In the web.config file :
<compilation debug="true>
had to be changed to
<compilation debug="true" targetFramework="4.0">
此错误还与缓存问题有关。
我遇到了同样的问题,只需清理并再次构建解决方案即可解决。
This error is also related with a cache issue.
I had the same problem and it was solved just cleaning and building the solution again.
我实际上正在寻找类似的错误,谷歌将我发送到这里来解决这个问题。
错误是:
我花了大约 15 分钟试图弄清楚它的用法中推断出来。它发生在 Razor .cshtml 视图文件内。
我必须对视图代码的部分进行注释才能到达它的咆哮之处,因为编译器没有多大帮助。
你能发现它吗?是啊……我又检查了大概两遍,一开始没明白!
看到 ViewModel 的属性只是
Zip
,而它应该是Organization.Zip
。就是这样。因此,请重新检查您的查看源代码...:-)
I was actually searching for a similar error and Google sent me here to this question.
The error was:
I spent maybe 15 minutes trying to figure it out. It was happening inside a Razor .cshtml view file.
I had to comment portions of the view code to get to where it was barking since the compiler didn't help much.
Can you spot it? Yeah... I re-checked it maybe twice and didn't get it at first!
See that the ViewModel's property is just
Zip
when it should beOrganization.Zip
. That was it.So re-check your view source code... :-)
C# 编译器只有 lambda
来推断 arg(TModel) 类型和 arg.MyProperty(TProperty) 类型。这是不可能的。
C# compiler have only lambda
for infer type of arg(TModel) an type of arg.MyProperty(TProperty). It's impossible.
如果有帮助的话,我在将
null
传递到通用TValue
的参数时遇到了这个问题,要解决这个问题,您必须强制转换 null 值:等等。
In case it helps, I've ran into this problem when passing
null
into a parameter for a genericTValue
, to get around this you have to cast your null values:etc.
只是为了添加另一个可能的解决方案/问题,我正在更新密码,但数据库模型没有任何密码属性,因此我创建了一个视图模型。
Just to add another possible solution/issue, I was updating the password but the DB Model did not have any Password Property, so I created a View Model.
您指的是类型而不是实例。将第二个和第四个代码示例中的示例中的“Model”设为小写。
应该是
You are referring to the type rather than the instance. Make 'Model' lowercase in the example in your second and fourth code samples.
should be