title: MVC3 Razor custom templates for primitive types and downcasting
body: <p>I've got a view model as:</p>
  
  <pre><code>public class AttributeEditorViewModel
  {
      public long AttributeId { get; set; }
      public string DisplayName { get; set; }
      public object Value { get; set; }
  }
  </code></pre>
  
  <p>The model is build using a model builder so that a final view model has a list of above objects:</p>
  
  <pre><code>public class AttributeListViewModel
  {
      public List&lt;AttributeEditorViewModel&gt; Attributes { get; set; }
  }
  </code></pre>
  
  <p>My model builder compiled a list of AttributeEditorViewModel object and sets appropriate primitive type to the Value property. The data type comes from separate definitions of attributes.</p>
  
  <p>Now the Razor view (lets say edit.cshtml) looks something like:</p>
  
  <pre><code>@model AttributeListViewModel
  
  @Html.EditorFor(m =&gt; m.Attributes)
  </code></pre>
  
  <p>and the editor template Views/Shared/EditorTemplates/AttributeEditorViewModel.cshtml:</p>
  
  <pre><code>@model AttributeEditorViewModel
  
  &lt;div&gt;
      &lt;label&gt;@Model.DisplayName&lt;/label&gt;
      @Html.EditorFor(m =&gt; m.Value)
      @Html.ValidationMessageFor(m =&gt; m.Value)
  &lt;/div&gt;
  </code></pre>
  
  <p>What I would expect; If I set the Value property of a AttributeEditorViewModel to a double, like so:</p>
  
  <pre><code>public ActionResult Edit()
  {
      return View(new AttributeListViewModel 
      {
          Attributes = new List&lt;AttributeEditorViewModel&gt; 
          {
              new AttributeEditorViewModel { DisplayName = "Double example", Value = (double) 0 }
          }
      };
  }
  </code></pre>
  
  <p>I would expect that a text box would be rendered. Instead it renders nothing. I've added another editor template Views/Shared/EditorTemplates/Double.cshtml:</p>
  
  <pre><code>@model double
  
  @Html.TextBoxFor(m =&gt; m)
  </code></pre>
  
  <p>Which solved the immediate problem. So now I get a textbox with value 0 in it. However using the EditorFor even inside the custom editor template again renders nothing.</p>
  
  <p>Is it fundamentally wrong to use object as base type for this? Why is it rendering nothing when EditorFor or EditorForModel is used?</p>
  
  <p>Any insight appreciated.</p>