Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 3: HtmlHelper.TextBoxFor(Expression<Func<MyModel,double?>>) Failing
    primarykey
    data
    text
    <p>Ok, so we've been converting an old project to .NET 4 and MVC 3, and it is mostly finished. I am now, however, receiving an error on a partial view when calling <code>HtmlHelper.TextBoxFor(Expression&lt;Func&lt;MyModel,double?&gt;&gt;)</code>. <code>HtmlHelper.TextBoxFor(Expression&lt;Func&lt;MyModel,string&gt;&gt;)</code> works just fine, however.</p> <p>Here are the relevant code snippets:</p> <p>A model class:</p> <pre><code>public class MyClass { public string SomeString { get; set; } public double? SomeNullableDouble { get; set; } } </code></pre> <p>In the main view ViewModel:</p> <pre><code>public class MyMainViewModel { public MyClass A { get; set; } public MyClass B { get; set; } } </code></pre> <p>In the Partial View ViewModel:</p> <pre><code>public class MyPartialViewModel { public Expression&lt;Func&lt;MyMainViewModel, string&gt;&gt; SomeStringProperty { get; set; } public Expression&lt;Func&lt;MyMainViewModel, double?&gt;&gt; SomeNullableDoubleProperty { get; set; } public MyPartialViewModel(Expression&lt;Func&lt;AnalyzeCostViewModel, string&gt;&gt; someStringProperty, Expression&lt;Func&lt;AnalyzeCostViewModel, double?&gt;&gt; someNullableDoubleProperty) { SomeStringProperty = someStringProperty; SomeNullableDoubleProperty = someNullableDoubleProperty; } } </code></pre> <p>In the Main View (We haven't bothered converting to Razor views just yet...):</p> <pre><code>&lt;div&gt; &lt;% Html.RenderPartial("MyPartialView", new MyPartialViewModel(m =&gt; m.A.SomeString, m =&gt; m.A.SomeNullableDouble)) %&gt; &lt;/div&gt; &lt;div&gt; &lt;% Html.RenderPartial("MyPartialView", new MyPartialViewModel(m =&gt; m.B.SomeString, m =&gt; m.B.SomeNullableDouble)) %&gt; &lt;/div&gt; </code></pre> <p>In the Partial View (MyPartialView.ascx):</p> <pre><code>&lt;p&gt; &lt;%=Model.Helper.LabelFor(Model.SomeStringProperty, "SomeLabel:") %&gt; &lt;%=Model.Helper.TextBoxFor(Model.SomeStringProperty)%&gt; &lt;%=Model.Helper.TextBoxFor(Model.SomeNullableDoubleProperty , new { @class = "some-class" })%&gt; &lt;/p&gt; </code></pre> <p>When trying to access the main view, I'm getting a compiler error:</p> <p>Server Error in '\' Application.</p> <p>Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.</p> <p>[InvalidOperationException: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.] System.Web.Mvc.ModelMetadata.FromLambdaExpression(Expression<code>1 expression, ViewDataDictionary</code>1 viewData) +522 System.Web.Mvc.Html.InputExtensions.TextBoxFor(HtmlHelper<code>1 htmlHelper, Expression</code>1 expression, IDictionary<code>2 htmlAttributes) +59 System.Web.Mvc.Html.InputExtensions.TextBoxFor(HtmlHelper</code>1 htmlHelper, Expression`1 expression) +50 ASP.views_(proprietary)._<em>Render</em>_control1(HtmlTextWriter __w, Control parameterContainer) in d:(proprietary)MyPartialView.ascx:37 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8 System.Web.UI.Control.Render(HtmlTextWriter writer) +10 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8 System.Web.UI.Page.Render(HtmlTextWriter writer) +29 System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +43 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060</p> <p>The error occurs in the partial view for the nullable double property expression, but not the string one.</p> <p>Is there an easy fix for this? Or is this something that will take some time to redesign?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    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