Created
January 28, 2010 07:02
-
-
Save actaneon/288509 to your computer and use it in GitHub Desktop.
List extensions useful for MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Web.Mvc; | |
public static class ListExtensions | |
{ | |
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> collection, Action<T> action) | |
{ | |
foreach (var item in collection) action(item); | |
return collection; | |
} | |
public static SelectList ToSelectList<T>(this IEnumerable<T> collection) | |
{ | |
return new SelectList(collection, "Key", "Value"); | |
} | |
public static SelectList ToSelectList<T>(this IEnumerable<T> collection, string selectedValue) | |
{ | |
return new SelectList(collection, "Key", "Value", selectedValue); | |
} | |
public static SelectList ToSelectList<T>(this IEnumerable<T> collection, | |
string dataValueField, string dataTextField) | |
{ | |
return new SelectList(collection, dataValueField, dataTextField); | |
} | |
public static SelectList ToSelectList<T>(this IEnumerable<T> collection, | |
string dataValueField, string dataTextField, string selectedValue) | |
{ | |
return new SelectList(collection, dataValueField, dataTextField, selectedValue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment