Note that there are some explanatory texts on larger screens.

plurals
  1. POThe type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly
    primarykey
    data
    text
    <p>Could someone please clarify something for me. In my ASP.NET MVC 2 app, I've got a <code>BaseViewModel</code> class which includes the following method: </p> <pre><code>public virtual IDictionary&lt;string, object&gt; GetHtmlAttributes&lt;TModel, TProperty&gt; (Expression&lt;Func&lt;TModel, TProperty&gt;&gt; propertyExpression) { return new Dictionary&lt;string, object&gt;(); } </code></pre> <p>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:</p> <pre><code>&lt;%: Html.TextBoxFor(model =&gt; model.MyProperty, Model.GetHtmlAttributes (model =&gt; model.MyProperty)) %&gt; </code></pre> <p>However when used as in the line above, I get a compilation error when I hit the view:</p> <blockquote> <p>The type arguments for method '<code>...BaseViewModel.GetHtmlAttributes&lt;TModel,TProperty&gt; Expression&lt;System.Func&lt;TModel,TProperty&gt;)</code>' cannot be inferred from the usage. Try specifying the type arguments explicitly.</p> </blockquote> <p>I have to do the following:</p> <pre><code>&lt;%: Html.TextBoxFor(model =&gt; model.MyProperty, Model.GetHtmlAttributes &lt;ChildModel, string&gt;(model =&gt; model.MyProperty)) %&gt; </code></pre> <p>I'm just looking for some clarity as to how it tries to infer the type, it has no problem doing so in the <code>HtmlHelper/TextBoxFor</code> extension method? </p> <p>Is it because <code>HtmlHelper</code> in the view will automatically be for the same type as is specified in the <code>ViewUserControl</code> at the top of the page, whereas my code can be for any type inheriting from <code>BaseViewModel</code>? Is is possible to write this in such a way that it can infer my model/property types?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload