无法从用法推断类型参数。尝试显式指定类型参数

发布于 2024-10-17 06:07:54 字数 1318 浏览 5 评论 0 原文

有人可以帮我澄清一下吗?在我的 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

小…红帽 2024-10-24 06:07:54

我知道这个问题已经有一个公认的答案,但对于我这个 .NET 初学者来说,有一个简单的解决方案可以解决我做错的事情,我想我会分享。

我一直在这样做:

@Html.HiddenFor(Model.Foo.Bar.ID)

对我有用的是更改为:(

@Html.HiddenFor(m => m.Foo.Bar.ID)

其中“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:

@Html.HiddenFor(Model.Foo.Bar.ID)

What worked for me was changing to this:

@Html.HiddenFor(m => m.Foo.Bar.ID)

(where "m" is an arbitrary string to represent the model object)

埖埖迣鎅 2024-10-24 06:07:54

在您的示例中,编译器无法知道 TModel 应该是什么类型。您可以做一些与您可能尝试使用扩展方法做的事情接近的事情。

static class ModelExtensions
{
   public static IDictionary<string, object> GetHtmlAttributes<TModel, TProperty>
      (this TModel model, Expression<Func<TModel, TProperty>> propertyExpression)
   {
       return new Dictionary<string, object>();
   }
}

但我认为你不可能拥有类似于虚拟的东西。

编辑:

实际上,您可以使用自引用泛型进行虚拟

class ModelBase<TModel>
{
    public virtual IDictionary<string, object> GetHtmlAttributes<TProperty>
        (Expression<Func<TModel, TProperty>> propertyExpression)
    {
        return new Dictionary<string, object>();
    }
}

class FooModel : ModelBase<FooModel>
{
    public override IDictionary<string, object> GetHtmlAttributes<TProperty>
        (Expression<Func<FooModel, TProperty>> propertyExpression)
    {
        return new Dictionary<string, object> { { "foo", "bar" } };
    }
}

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.

static class ModelExtensions
{
   public static IDictionary<string, object> GetHtmlAttributes<TModel, TProperty>
      (this TModel model, Expression<Func<TModel, TProperty>> propertyExpression)
   {
       return new Dictionary<string, object>();
   }
}

But you wouldn't be able to have anything similar to virtual, I think.

EDIT:

Actually, you can do virtual, using self-referential generics:

class ModelBase<TModel>
{
    public virtual IDictionary<string, object> GetHtmlAttributes<TProperty>
        (Expression<Func<TModel, TProperty>> propertyExpression)
    {
        return new Dictionary<string, object>();
    }
}

class FooModel : ModelBase<FooModel>
{
    public override IDictionary<string, object> GetHtmlAttributes<TProperty>
        (Expression<Func<FooModel, TProperty>> propertyExpression)
    {
        return new Dictionary<string, object> { { "foo", "bar" } };
    }
}
简单爱 2024-10-24 06:07:54

我也遇到了同样的问题,我的解决方案:
在 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">

笑红尘 2024-10-24 06:07:54

此错误还与缓存问题有关。

我遇到了同样的问题,只需清理并再次构建解决方案即可解决。

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.

活雷疯 2024-10-24 06:07:54

我实际上正在寻找类似的错误,谷歌将我发送到这里来解决这个问题。
错误是:

方法的类型参数
'IModelExpressionProvider.CreateModelExpression(ViewDataDictionary,表达式>)'
无法从使用情况推断

我花了大约 15 分钟试图弄清楚它的用法中推断出来。它发生在 Razor .cshtml 视图文件内。
我必须对视图代码的部分进行注释才能到达它的咆哮之处,因为编译器没有多大帮助。

<div class="form-group col-2">
    <label asp-for="Organization.Zip"></label>
    <input asp-for="Organization.Zip" class="form-control">
    <span asp-validation-for="Zip" class="color-type-alert"></span>
</div>

你能发现它吗?是啊……我又检查了大概两遍,一开始没明白!

看到 ViewModel 的属性只是 Zip,而它应该是 Organization.Zip。就是这样。

因此,请重新检查您的查看源代码...:-)

I was actually searching for a similar error and Google sent me here to this question.
The error was:

The type arguments for method
'IModelExpressionProvider.CreateModelExpression(ViewDataDictionary, Expression>)'
cannot be inferred from the usage

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.

<div class="form-group col-2">
    <label asp-for="Organization.Zip"></label>
    <input asp-for="Organization.Zip" class="form-control">
    <span asp-validation-for="Zip" class="color-type-alert"></span>
</div>

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 be Organization.Zip. That was it.

So re-check your view source code... :-)

隱形的亼 2024-10-24 06:07:54

C# 编译器只有 lambda

arg => arg.MyProperty

来推断 arg(TModel) 类型和 arg.MyProperty(TProperty) 类型。这是不可能的。

C# compiler have only lambda

arg => arg.MyProperty

for infer type of arg(TModel) an type of arg.MyProperty(TProperty). It's impossible.

眼趣 2024-10-24 06:07:54

如果有帮助的话,我在将 null 传递到通用 TValue 的参数时遇到了这个问题,要解决这个问题,您必须强制转换 null 值:

(string)null

(int)null

等等。

In case it helps, I've ran into this problem when passing null into a parameter for a generic TValue, to get around this you have to cast your null values:

(string)null

(int)null

etc.

苏大泽ㄣ 2024-10-24 06:07:54

只是为了添加另一个可能的解决方案/问题,我正在更新密码,但数据库模型没有任何密码属性,因此我创建了一个视图模型。

 <div class="form-group">
            <label asp-for="user.Password"></label>
            <input asp-for="user.Password" class="form-control" />
            <span asp-validation-for="user.Password" class="text-danger"></span>
  </div>

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.

 <div class="form-group">
            <label asp-for="user.Password"></label>
            <input asp-for="user.Password" class="form-control" />
            <span asp-validation-for="user.Password" class="text-danger"></span>
  </div>
桃扇骨 2024-10-24 06:07:54

您指的是类型而不是实例。将第二个和第四个代码示例中的示例中的“Model”设为小写。

Model.GetHtmlAttributes

应该是

model.GetHtmlAttributes

You are referring to the type rather than the instance. Make 'Model' lowercase in the example in your second and fourth code samples.

Model.GetHtmlAttributes

should be

model.GetHtmlAttributes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文