I have a CSV file on a remote server that is accessible via a URL such as: http://mydomain.com/test.csv. It is simply records of a key/value pair. In my controller, how would I consume that CSV file and pass to the view as a model? I am a little new to MVC3 so I would appreciate any help.
Here is a sample of my CSV:
key,value
Key1,ValueA
Key2,ValueB
Key3,ValueC
Key4,ValueD
I wouldn’t call it from the controller. I would delegate that logic to a service, using interface-driven development.
A quick google yields plenty of results for a CSV parser. So it’s just a matter of constructing a HTTP request, parsing the CSV and then mapping it to a ViewModel.
So your controller could look like this:
This way, if you decide to use a different CSV parser (or roll your own), your Controller need not change. And you can re-use this service across your application.