Export database data to csv from view by date range asp.net mvc3
- by Benjamin Randal
I am trying to find a way to export data from my database and save it as a .csv file. Ideally the user will be able to select a date range on a view, which will display the data to be exported, then the user can click an "export to CSV" link. I've done quite a bit of searching and but have not found much specific enough to help me step through the process. Any help would be great.
I would like to export data from this database model...
{
public class InspectionInfo
{
[Key]
public int InspectionId { get; set; }
[DisplayName("Date Submitted")]
[DataType(DataType.Date)]
// [Required]
public DateTime Submitted { get; set; }
[DataType(DataType.MultilineText)]
[MaxLength(1000)]
// [Required]
public string Comments { get; set; }
// [Required]
public Contact Contact { get; set; }
[ForeignKey("Contact")]
public Int32 ContactId { get; set; }
[MaxLength(100)]
public String OtherContact { get; set; }
I have a service for search also, just having difficulty implementing
public SearchResults SearchInspections(SearchRequest request)
{
using (var db = new InspectionEntities())
{
var results = db.InspectionInfos
.Where( i=>
(
(null == request.StartDate || i.Submitted >= request.StartDate.Value) &&
(null == request.EndDate || i.Submitted <= request.EndDate.Value)
)
)
.OrderBy(i=>i.Submitted)
.Skip(request.PageSize*request.PageIndex).Take(request.PageSize);
return new SearchResults{
TotalResults=results.Count(),
PageIndex=request.PageIndex,
Inspections=results.ToList(),
SearchRequest=request
};
}
}