简体   繁体   中英

API controller passing data to mvc controller

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",

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM