Asp.Net MVC: SelectList extension methods

I wanted to share some extensions I use for dealing with the Asp.Net MVC select lists (DropDownList/ListBox) helpers.  Why the mvc helper apis are not more friendly in this area, I do not know…

Now some examples of how to use these extensions:

A single dropdown:

@Html.DropDownListFor(x => x.PropertyId, Model.AllProperties.ToSelectListItems(x => x.Name, x => x.Id.ToString(), x => x.Id == Model.PropertyId))

A multiselect dropdown:

@Html.ListBoxFor(x => x.PropertyIds, Model.AllProperties.ToMultiSelectListItems(x => x.Name, x => x.Id.ToString(), x => Model.PropertyIds.Contains(x.Id)))

 

7 thoughts on “Asp.Net MVC: SelectList extension methods

  1. Cannot even compile the code:

    ‘System.Collections.Generic.IEnumerable’ does not contain a definition for ‘ToSelectListItems’ and the best extension method overload ‘SelectExtensionTest.Infrastructure.EnumerableSelectListExtensions.ToSelectListItems(System.Collections.Generic.IEnumerable, System.Func, System.Func, string)’ has some invalid arguments

    And

    Argument 4: cannot convert from ‘bool’ to ‘string’

    1. I updated the post with my latest version of these helpers. Sorry for the previous version if it didn’t compile correctly. Sometimes I change variables when I copy & paste to the blog and I’m guessing that happened.

      As far as the error about ‘System.Collections.Generic.IEnumerable’ does not contain a definition for ‘ToSelectListItems’ and the best extension method overload: Sounds like you need to add the namespace for these extensions to your web.config or as an import statement on your view.

      Hope that helps and let me know if you continue to have troubles

  2. This looks very interesting! I have beeb struggling a bit with DropDownLists, and I managed to find a way to work with them in ASP.Net MVC, but this looks much more elegant.

    One question though. ASP’.Net MVC is quite new to me and I would like to know where this code should reside in a typical project, and how you did to make Model.AllProperties inherit these methods.

    Cheers!

    JF

    1. I place files like this in a private assembly shared between all of my projects. For a single project, though, maybe /Infrastructure/Extensions/EnumerableExtensions.cs would suffice. You’ll need to add the following using statements to your cs file:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web.Mvc;

  3. Hi Jim!

    The reason I am missing something is that your code got garbled when you posted it. For example Func should be Func, List should be List, etc. in your post. Everything between got interpreted as an HTML tag and disappeared. I was able to recuperate the source code by looking at the source of this web page, and this fixed half of the code. All the code about MultiSelectListItems is missing parts event in the source of the HTML. Would it be possible to encode HTML entities in your code and to repost it? You can use an online encoder like this one for this: http://www.opinionatedgeek.com/dotnet/tools/htmlencode/encode.aspx

    Cheers!

    JF

    1. Thanks for letting me know about the code. I replaced it with a github gist. That should help with readability, highlighting, and collaboration, if anyone wants to take these further.

      Please let me know if you run into anything else.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>