Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Actually, you just have to pass in <strong><code>null</code></strong> for the <code>Html.DropDownList()</code>.<br /> I was having the same exact problem, and used the Reflector to look at the MVC Source Code.</p> <p>In the <code>System.Web.Mvc.Extensions.SelectExtensions</code> class's <code>SelectInternal()</code> method, it checks whether the <strong>selectList</strong> parameter is <strong>null</strong> or not. If it is passed in as <code>null</code>, it looks up the <code>SelectList</code> properly.</p> <p>Here is the "Code-behind".</p> <pre><code>ViewData["MyDropDown"] = new SelectList(selectListItems, "Value", "Text", selectedValue.ToString() ); </code></pre> <p>Here is the HTML view code.</p> <pre><code>&lt;%= Html.DropDownList("MyDropDown", null, "** Please Select **", new { @class = "my-select-css-class" } ) %&gt; </code></pre> <p>Note: I'm using ASP.NET MVC 2.0 (Beta Version).</p> <h2>UPDATE NOTE: January 31st 2012</h2> <p>After extensively using ASP.NET MVC for the past 3 years, I prefer using <strong><code>additionalViewData</code></strong> from the <a href="http://msdn.microsoft.com/en-us/library/ie/ff406462.aspx" rel="nofollow noreferrer"><code>Html.EditorFor()</code> method</a> more. </p> <p>Pass in your <strong>[List Items]</strong> as an <code>anonymous</code> object with the <strong>same property name</strong> as the Model's property into the <code>Html.EditorFor()</code> method. </p> <pre><code>&lt;%= Html.EditorFor( m =&gt; m.MyPropertyName, new { MyPropertyName = Model.ListItemsForMyPropertyName } ) %&gt; </code></pre> <p>If you want more details, please refer to <a href="https://stackoverflow.com/questions/4087815/how-to-pass-data-to-the-view-in-mvc-asp-net/4099487#4099487">my answer in another thread here</a>.</p>
 

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