<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" version="2.0">
  <channel>
    <title>Paul Mrozowski's Blog - ASP.NET MVC</title>
    <link>http://www.rcs-solutions.com/blog/</link>
    <description>A day in the life (of a developer)</description>
    <language>en-us</language>
    <copyright>Paul Mrozowski / RCS Solutions, Inc.</copyright>
    <lastBuildDate>Sat, 09 Oct 2010 15:53:27 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>paulm@rcs-solutions.com</managingEditor>
    <webMaster>paulm@rcs-solutions.com</webMaster>
    <item>
      <trackback:ping>http://www.rcs-solutions.com/blog/Trackback.aspx?guid=fef6ca36-4faa-4b5e-b76c-a1052203b701</trackback:ping>
      <pingback:server>http://www.rcs-solutions.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.rcs-solutions.com/blog/PermaLink,guid,fef6ca36-4faa-4b5e-b76c-a1052203b701.aspx</pingback:target>
      <dc:creator>Paul Mrozowski</dc:creator>
      <wfw:comment>http://www.rcs-solutions.com/blog/CommentView,guid,fef6ca36-4faa-4b5e-b76c-a1052203b701.aspx</wfw:comment>
      <wfw:commentRss>http://www.rcs-solutions.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=fef6ca36-4faa-4b5e-b76c-a1052203b701</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I’ve recently started a new ASP.NET MVC project and ran across what is a really common
need: I wanted a drop-down list that contained the same items but that I could re-use
a few times on the same page. In “classic” ASP.NET I’d just create a user control,
expose a property or two to allow me to handle data binding to this list and be done
with it. Heck, in my case it didn’t even need to be data driven – it was a static
list of items (like a drop-down list of states). I could do this inside of a view
then populate the list but I didn’t want to tie this to a particular view, I wanted
it available from any page.
</p>
        <p>
OK, so how do you do this in ASP.NET MVC? It doesn’t have the same control model,
so I wasn’t entirely sure what the best way to handle this was. 
</p>
        <p>
Some things I considered:
</p>
        <ul>
          <li>
Create my own “helper” that generated all the HTML for it. That sounded like way more
work than I wanted to do. 
</li>
          <li>
Create my own helper but somehow leverage the built-in DropDownList to do the heavy
lifting. I started looking at this, didn’t see immediately how I would hook everything
together so I skipped it. 
</li>
          <li>
Create a partial view. This was actually my first thought but I couldn’t figure out
how I’d handle being able to “name” the DropDownList in the partial view (since I
needed to have multiple copies of the control on the same page and I wanted each to
have it’s own unique name and bind to a different property in my ViewModel. Mind you,
at this point I have like 3 hours experience with MVC. 
</li>
        </ul>
        <p>
 
</p>
        <p>
Honestly, I wasn’t happy with any of them. They all seemed like more work than I was
expecting (but at this point I wasn’t sure how much work was really required). I did
a bunch of Google searches and finally ran across this question on Stack Overflow:
</p>
        <p>
          <a title="http://stackoverflow.com/questions/289048/asp-net-mvc-us-state-drop-down-list" href="http://stackoverflow.com/questions/289048/asp-net-mvc-us-state-drop-down-list">http://stackoverflow.com/questions/289048/asp-net-mvc-us-state-drop-down-list</a>
        </p>
        <p>
That was exactly what I was looking for. You create a simple static method that returns
an IDictionary and then another one which returns a SelectList of your IDictionary
so that it can be used by the framework.
</p>
        <div style="border-bottom: #000080 1px solid; border-left: #000080 1px solid; font-family: 'Courier New', courier, monospace; color: #000; font-size: 10pt; border-top: #000080 1px solid; border-right: #000080 1px solid">
          <div style="padding-bottom: 2px; background-color: #dfdfdf; padding-left: 5px; padding-right: 5px; max-height: 300px; overflow: auto; padding-top: 2px">
            <span style="color: #0000ff">public</span>
            <span style="color: #0000ff">class</span>
            <span style="color: #2b91af">ItemScores</span>
            <br />
{ 
<br />
    <span style="color: #0000ff">public</span><span style="color: #0000ff">static</span><span style="color: #0000ff">readonly</span><span style="color: #2b91af">IDictionary</span>&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">string</span>&gt;
Scores = <span style="color: #0000ff">new</span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">string</span>&gt;() 
<br />
        { 
<br />
            {<span style="background: #e5e5e5">"5
- Best"</span>, <span style="background: #e5e5e5">"1"}</span>, 
<br />
            {<span style="background: #e5e5e5">"4"</span>, <span style="background: #e5e5e5">"4"}</span>, 
<br />
            {<span style="background: #e5e5e5">"3
- Average"</span>, <span style="background: #e5e5e5">"3"}</span>, 
<br />
            {<span style="background: #e5e5e5">"2"</span>, <span style="background: #e5e5e5">"2"}</span>, 
<br />
            {<span style="background: #e5e5e5">"1
- Bad"</span>, <span style="background: #e5e5e5">"1"}</span><br />
        }; 
<br /><br />
    <span style="color: #0000ff">public</span><span style="color: #0000ff">static</span> SelectList
ScoreList 
<br />
    { 
<br />
        <span style="color: #0000ff">get</span> { <span style="color: #0000ff">return</span><span style="color: #0000ff">new</span> SelectList(Scores, <span style="background: #e5e5e5">"Value"</span>, <span style="background: #e5e5e5">"Key"</span>);
} 
<br />
    } 
<br />
}
</div>
        </div>
        <p>
Then from within your view you use the standard Html helper:
</p>
        <pre>
          <p>
&lt;%: Html.DropDownList("OverallScore", ItemScores.ScoreList) %&gt;
</p>
        </pre>
        <p>
That was a lot closer to what I was expecting. I expect to revisit this topic again
as soon as I need a slightly more complex component, but for right now this works
well for me.
</p>
        <img width="0" height="0" src="http://www.rcs-solutions.com/blog/aggbug.ashx?id=fef6ca36-4faa-4b5e-b76c-a1052203b701" />
      </body>
      <title>Creating a Simple &amp;amp; Re-Usable Dropdownlist/Combo in ASP.NET MVC</title>
      <guid isPermaLink="false">http://www.rcs-solutions.com/blog/PermaLink,guid,fef6ca36-4faa-4b5e-b76c-a1052203b701.aspx</guid>
      <link>http://www.rcs-solutions.com/blog/2010/10/09/CreatingASimpleAmpReUsableDropdownlistComboInASPNETMVC.aspx</link>
      <pubDate>Sat, 09 Oct 2010 15:53:27 GMT</pubDate>
      <description>&lt;p&gt;
I’ve recently started a new ASP.NET MVC project and ran across what is a really common
need: I wanted a drop-down list that contained the same items but that I could re-use
a few times on the same page. In “classic” ASP.NET I’d just create a user control,
expose a property or two to allow me to handle data binding to this list and be done
with it. Heck, in my case it didn’t even need to be data driven – it was a static
list of items (like a drop-down list of states). I could do this inside of a view
then populate the list but I didn’t want to tie this to a particular view, I wanted
it available from any page.
&lt;/p&gt;
&lt;p&gt;
OK, so how do you do this in ASP.NET MVC? It doesn’t have the same control model,
so I wasn’t entirely sure what the best way to handle this was. 
&lt;/p&gt;
&lt;p&gt;
Some things I considered:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Create my own “helper” that generated all the HTML for it. That sounded like way more
work than I wanted to do. 
&lt;/li&gt;
&lt;li&gt;
Create my own helper but somehow leverage the built-in DropDownList to do the heavy
lifting. I started looking at this, didn’t see immediately how I would hook everything
together so I skipped it. 
&lt;/li&gt;
&lt;li&gt;
Create a partial view. This was actually my first thought but I couldn’t figure out
how I’d handle being able to “name” the DropDownList in the partial view (since I
needed to have multiple copies of the control on the same page and I wanted each to
have it’s own unique name and bind to a different property in my ViewModel. Mind you,
at this point I have like 3 hours experience with MVC. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Honestly, I wasn’t happy with any of them. They all seemed like more work than I was
expecting (but at this point I wasn’t sure how much work was really required). I did
a bunch of Google searches and finally ran across this question on Stack Overflow:
&lt;/p&gt;
&lt;p&gt;
&lt;a title="http://stackoverflow.com/questions/289048/asp-net-mvc-us-state-drop-down-list" href="http://stackoverflow.com/questions/289048/asp-net-mvc-us-state-drop-down-list"&gt;http://stackoverflow.com/questions/289048/asp-net-mvc-us-state-drop-down-list&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
That was exactly what I was looking for. You create a simple static method that returns
an IDictionary and then another one which returns a SelectList of your IDictionary
so that it can be used by the framework.
&lt;/p&gt;
&lt;div style="border-bottom: #000080 1px solid; border-left: #000080 1px solid; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; color: #000; font-size: 10pt; border-top: #000080 1px solid; border-right: #000080 1px solid"&gt;
&lt;div style="padding-bottom: 2px; background-color: #dfdfdf; padding-left: 5px; padding-right: 5px; max-height: 300px; overflow: auto; padding-top: 2px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ItemScores&lt;/span&gt; 
&lt;br /&gt;
{ 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IDictionary&lt;/span&gt;&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;
Scores = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;() 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;span style="background: #e5e5e5"&gt;&amp;quot;5
- Best&amp;quot;&lt;/span&gt;, &lt;span style="background: #e5e5e5"&gt;&amp;quot;1&amp;quot;}&lt;/span&gt;, 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;span style="background: #e5e5e5"&gt;&amp;quot;4&amp;quot;&lt;/span&gt;, &lt;span style="background: #e5e5e5"&gt;&amp;quot;4&amp;quot;}&lt;/span&gt;, 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;span style="background: #e5e5e5"&gt;&amp;quot;3
- Average&amp;quot;&lt;/span&gt;, &lt;span style="background: #e5e5e5"&gt;&amp;quot;3&amp;quot;}&lt;/span&gt;, 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;span style="background: #e5e5e5"&gt;&amp;quot;2&amp;quot;&lt;/span&gt;, &lt;span style="background: #e5e5e5"&gt;&amp;quot;2&amp;quot;}&lt;/span&gt;, 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;span style="background: #e5e5e5"&gt;&amp;quot;1
- Bad&amp;quot;&lt;/span&gt;, &lt;span style="background: #e5e5e5"&gt;&amp;quot;1&amp;quot;}&lt;/span&gt; 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }; 
&lt;br /&gt;
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; SelectList
ScoreList 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160; { 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0000ff"&gt;get&lt;/span&gt; { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SelectList(Scores, &lt;span style="background: #e5e5e5"&gt;&amp;quot;Value&amp;quot;&lt;/span&gt;, &lt;span style="background: #e5e5e5"&gt;&amp;quot;Key&amp;quot;&lt;/span&gt;);
} 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160; } 
&lt;br /&gt;
}
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Then from within your view you use the standard Html helper:
&lt;/p&gt;
&lt;pre&gt;
&lt;p&gt;
&amp;lt;%: Html.DropDownList(&amp;quot;OverallScore&amp;quot;, ItemScores.ScoreList) %&amp;gt;
&lt;/p&gt;
&lt;/pre&gt;
&lt;p&gt;
That was a lot closer to what I was expecting. I expect to revisit this topic again
as soon as I need a slightly more complex component, but for right now this works
well for me.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.rcs-solutions.com/blog/aggbug.ashx?id=fef6ca36-4faa-4b5e-b76c-a1052203b701" /&gt;</description>
      <comments>http://www.rcs-solutions.com/blog/CommentView,guid,fef6ca36-4faa-4b5e-b76c-a1052203b701.aspx</comments>
      <category>ASP.NET MVC</category>
    </item>
  </channel>
</rss>