Skip to content

Instantly share code, notes, and snippets.

@derans
derans / ExcelFileResult.cs
Created September 23, 2012 22:17
Excel Action Result
public class ExcelFileResult<T> : ActionResult
{
private readonly IEnumerable<T> _records;
public ExcelFileResult(IEnumerable<T> records)
{
_records = records;
}
public override void ExecuteResult(ControllerContext context)
@derans
derans / ExcelColumnDefinition.cs
Created September 23, 2012 22:15
Excel Column Definition Class
public class ExcelColumnDefinition
{
public MemberInfo MemberInfo { get; set; }
public string Format { get; set; }
public string Header { get; set; }
public static ExcelColumnDefinition Create<T>(Expression<Func<T, object>> member, string format = null, string header = null)
{
return new ExcelColumnDefinition { MemberInfo = GetMemberInfo(member), Format = format, Header = header };
}
@derans
derans / Specific_columns_and_formatting_with_method.cs
Created September 23, 2012 22:14
Specific Columns & Formatting with Helper Method
public ActionResult ExportToExcel()
{
var records = _getSampleInfoQuery.Execute();
var columns = new[]
{
Column(x => x.College),
Column(x => x.Amount, ExcelFormat.Money),
Column(x => x.CreatedDate, ExcelFormat.Date, "Date"),
Column(x => x.CreatedBy),
Column(x => x.CreatedDate, ExcelFormat.Time, "Time"),
@derans
derans / column_helper_method.cs
Created September 23, 2012 22:12
Column Helper Method
private static ExcelColumnDefinition Column(Expression<Func<SampleInfo, object>> member, string format = null, string header = null)
{
return ExcelColumnDefinition.Create(member, format, header);
}
@derans
derans / Specific_columns_and_formatting_export.cs
Created September 23, 2012 22:10
Specific Columns & Formatting ExportToExcel
public ActionResult ExportToExcel()
{
var records = _getSampleInfoQuery.Execute();
var columns = new[]
{
ExcelColumnDefinition.Create<SampleInfo>(x => x.College),
ExcelColumnDefinition.Create<SampleInfo>(x => x.Amount, ExcelFormat.Money),
ExcelColumnDefinition.Create<SampleInfo>(x => x.CreatedDate, ExcelFormat.Date, "Date"),
ExcelColumnDefinition.Create<SampleInfo>(x => x.CreatedBy),
ExcelColumnDefinition.Create<SampleInfo>(x => x.CreatedDate, ExcelFormat.Time, "Time"),
public ActionResult ExportToExcel()
{
var records = _getSampleInfoQuery.Execute();
return new ExcelFileResult<SampleInfo>(records);
}
@derans
derans / css_toggle.html
Created August 30, 2012 05:18
CSS Toggle
<html>
<head>
<style type="text/css">
.toggle-me { display: none; }
div:hover .toggle-me { display: inline-block; }
</style>
<body>
<div>Most Important <span class="toggle-me">More Information</span></div>
</body>
</html>
@derans
derans / DropDownListFor_SelectListProviderAttribute.cs
Created August 25, 2012 07:34
DropDownList HtmlHelper Extension
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression) where TModel : class
{
var property = expression.GetProperty();
IEnumerable<SelectListItem> selectList;
var selectListProviderAttribute = property.GetAttribute<SelectListProviderAttribute>();
if (selectListProviderAttribute != null)
{
var provider = selectListProviderAttribute.Provider;
@derans
derans / CommunitySelectListProvider.cs
Created August 25, 2012 07:31
ISelectListProvider implementation example
public class CommunitySelectListProvider : ISelectListProvider
{
private readonly IGetCommunitiesQuery _getCommunitiesQuery;
public CommunitySelectListProvider(IGetCommunitiesQuery getCommunitiesQuery)
{
_getCommunitiesQuery = getCommunitiesQuery;
}
public IEnumerable<SelectListItem> Provide()
@derans
derans / Select_list_provider_interface
Created August 25, 2012 06:53
Select List Provider Interface
public interface ISelectListProvider
{
IEnumerable<SelectListItem> Provide();
}