0

I have been struggling on trying to work this for the last 3 days, how do you pass data from an api controller into an mvc controller and use the data to populate a selectlistitem.

I have seen plenty of examples of calling the api from the webpage which is all well and good, but what if the user has javascript disabled, it will not display the data.

So any help with an example for this would be much appreciated.

My code is:

web.api

    public IEnumerable<DisplayCurrencyInDDL> GetCurrencyForDDL()
                {
                    var s = _ICurr.InsetCurrencyIntoDataBase();
                    return s.AsEnumerable(); 
                }

mvc controller

    WebClient wc = new WebClient();
    var s = wc.DownloadString("http://localhost:50687/api/Currency");

How do I get the value from var s (currency and currencyid) into a selectlistitem.

Thanks George

edit data returned as: [ { "strCountry": "Afghan Afghani", "strCountryCode": "AFN" },    { "strCountry": "Albanian Lek", "strCountryCode": "ALL" }, { "strCountry": "Algerian Dinar", "strCountryCode": "DZD" }, { "strCountry": "Andorra Euro1",
tereško
  • 58,060
  • 25
  • 98
  • 150
CareerChange
  • 669
  • 4
  • 17
  • 34
  • What is the format of var s ? Can you post a sample here? – Ravi Y Nov 21 '12 at 11:21
  • Hi @ryadavilli sample code [ { "strCountry": "Afghan Afghani", "strCountryCode": "AFN" }, { "strCountry": "Albanian Lek", "strCountryCode": "ALL" }, { "strCountry": "Algerian Dinar", "strCountryCode": "DZD" }, { "strCountry": "Andorra Euro", – CareerChange Nov 21 '12 at 11:32

2 Answers2

4

I don't understand why you are doing it this way.

If you want to share some code you can do this by moving the code into some Library and instantiate that class in WebAPI and also in your MVC Controller.

Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
  • this is where i'm getting confused with web api, if i'm just using the data for my own site, then I do not need web api, but if i'm going to share data then api is the way to go. – CareerChange Nov 21 '12 at 12:03
  • WebApi is good way to call API but only if your calling the api from some remote location not from within your project. – Asif Mushtaq Nov 22 '12 at 04:40
  • Hi @Asif thanks for the clarification, all examples I've seen involve returning data to your own site, thats why i was getting confused. So really i do not need to use it – CareerChange Nov 22 '12 at 08:27
0

Ok, so after reading this post on stackoverflow difference between apiController and controller

Its my understanding that if i'm returning data to my own website, then use mvc controller, but if i'm allowing a 3rd party to consume data from my site, then put the data in an api controller.

Also if a user visited my site/your site and had javascript disabled, then json would not work on the client side as requires jQuery etc, so my understanding is use api if you are sure the visitor will not have javascript disabled.

Please let me know if that correct

Community
  • 1
  • 1
CareerChange
  • 669
  • 4
  • 17
  • 34
  • What do you mean by "returning data to my own website"? Retrieving data form certain URLs (via AJAX calls or simple links), or retrieving data "back end" and displaying? Web API is made to easily enable you to create - well - an API for others (or yourself) to use to access your site/application. If you are just going to get some data from your own database to display, you do not need to do any of this. Just get the data in objects in your controller and pass them into your MVC views, and you're done. – Arve Systad Nov 21 '12 at 22:50