Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 954609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:14:37+00:00 2026-05-16T00:14:37+00:00

I have a scenario where the user have the option to click the button

  • 0

I have a scenario where the user have the option to click the button “download” and I should create a csv file, that contains history data, and then let the user save the file locally. As I haven’t done this before, I started to look around for how to do this and basically came across both Custom View Engine, and Custom Action Result.

My question now is what is the benefit/disadvantages with these? What is the preferred way to go?

The CSV file, is basically just containing headers, and data (up to about 15 columns/fields) with a few thousand rows. so nothing special really.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:14:38+00:00Added an answer on May 16, 2026 at 12:14 am

    I would probably go for a custom action result (the actual serialization in my example is done with FileHelpers):

    public class CsvResult<T> : ActionResult
    {
        public IEnumerable<T> Records { get; private set; }
        public CsvResult(IEnumerable<T> records)
        {
            Records = records;
        }
    
        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.HttpContext.Response;
            response.ContentType = "text/csv";
            var engine = new FileHelperEngine(typeof(T));
            engine.WriteStream(response.Output, Records);
        }
    }
    
    [DelimitedRecord(",")] 
    public class Customer 
    {
        public int Id { get; set; }
        public string Name { get; set; }         
    }
    
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var customers = new[]
            {
                new Customer { Id = 1, Name = "customer 1" },
                new Customer { Id = 2, Name = "customer 2" },
            };
            return new CsvResult<Customer>(customers);
        }
    }
    

    You could even prettify this return statement (generics are ugly and superfluous in this case) by using an extension method:

    public static class ControllerExtensions
    {
        public static ActionResult Csv<T>(this Controller controller, IEnumerable<T> records)
        {
            return new CsvResult<T>(records);
        }
    }
    

    and then simply:

    public ActionResult Index()
    {
        var customers = new[]
        {
            new Customer { Id = 1, Name = "customer 1" },
            new Customer { Id = 2, Name = "customer 2" },
        };
        return this.Csv(customers);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following scenario: I have various user's data stored in my database.
Consider following scenario: I have RESTful URL /articles that returns list of articles user
I have the following scenario: in activity A, when a user clicks a button,
Consider the following scenario We have a simple database that involves two entities: user
I have a fancybox that is shown when a user clicks a button. The
I have a scenario where a user will have access to a one-time-url. When
I have a scenario where a user is emailing another user in an HTML
I have a scenario where a user submits a start and stop date for
Following scenario: I have user object: ... @Column(name = TITLE) private String title; @NotNull
I will like to know: I have a scenario. If a user adds a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.