title: Passing SelectList through ViewData to editor templates - not displayed properly
body: <p>It's a bit complicated so bear with me.</p>
  
  <p>Let's say I've got an example of a controller edit action defined like:</p>
  
  <pre><code>Node nd = _repo.getNode(id);
  
  List&lt;Category&gt; ac = new List&lt;Category&gt;();
  ac.AddRange(_repo.getCategories());
  SelectList acl = new SelectList(ac, "category_id", "category_name", ac.Where(cat =&gt; cat.category_id == nd.category_id).First());
  
  ViewData["category_id"] = acl;
  
  return View(nd);
  </code></pre>
  
  <p>The view is templated like so:</p>
  
  <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;Myapp.Models.Node&gt;" %&gt;
  &lt;% if (ViewData.TemplateInfo.TemplateDepth &gt; 1) 
     { %&gt;
  &lt;%= ViewData.ModelMetadata.SimpleDisplayText %&gt;
  &lt;% }
     else
     { %&gt;
  &lt;table cellpadding="0" cellspacing="0" border="0"&gt;
      &lt;% foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm =&gt; pm.ShowForEdit &amp;&amp; !ViewData.TemplateInfo.Visited(pm)))
         { %&gt;
      &lt;% if (prop.HideSurroundingHtml)
         { %&gt;
      &lt;%= Html.Editor(prop.PropertyName) %&gt;
      &lt;% }
         else
         { %&gt;
  
      &lt;tr&gt;
          &lt;td&gt;
              &lt;div class="editor-label" style="text-align: right;"&gt;
                  &lt;%= prop.IsRequired ? "*" : ""%&gt;
                  &lt;%= Html.Label(prop.PropertyName)%&gt;
              &lt;/div&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;div class="editor-field"&gt;
                  &lt;% if (ViewData.Keys.Contains(prop.PropertyName))
                     {
                         if ((ViewData[prop.PropertyName]).GetType().Name == "SelectList")
                              { %&gt;
                                  &lt;%= Html.DropDownList(prop.PropertyName, (SelectList)ViewData[prop.PropertyName])%&gt;
                              &lt;% }
                                  else
                                      { %&gt;
                                          &lt;%= Html.Editor(prop.PropertyName)%&gt;
                                      &lt;% } %&gt;                       
                    &lt;% }
                     else
                     { %&gt;
                  &lt;%= Html.Editor(prop.PropertyName)%&gt;
                  &lt;% } %&gt;
                  &lt;%= Html.ValidationMessage(prop.PropertyName, "*")%&gt;
              &lt;/div&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;% } %&gt;
      &lt;% } %&gt;
  &lt;/table&gt;
  &lt;% } %&gt;
  </code></pre>
  
  <p>So, what the template does is display a dropdown list for every property for which  <code>ViewData["property_name"]</code> exists.</p>
  
  <p>I've also defined <code>DisplayName</code> metadata attributes for every property of my Node class.</p>
  
  <p>Now, the dropdown lists display fine and are being populated correctly, but:</p>
  
  <ol>
  <li>The first value from a list is always selected, even though the SelectList selected value predicate is fine and does set a proper value (in the debugger at least).</li>
  <li>Html.Label in the template returns a proper DisplayName for properties, but when I define a ViewData for them so as to display the dropdown list, the label resets to normal property name (ie. <code>category_id</code> instead of <code>Category</code>).</li>
  </ol>
  
  <p>What gives? Can you think of any "neater" way to accomplish this functionality?</p>