简体   繁体   English

API控制器将数据传递到MVC控制器

[英]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. 在过去的三天里,我一直在努力工作,如何将数据从api控制器传递到mvc控制器,并使用数据填充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. 我已经看到了很多从网页调用api的示例,这些示例都很好,但是如果用户禁用了javascript怎么办,它将不会显示数据。

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 MVC控制器

    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. 如何将var的值(货币和currencyid)转换为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 . 如果要共享一些代码,可以通过将代码移到某些库中并在WebAPIMVC Controller实例化该类来WebAPI

Ok, so after reading this post on stackoverflow difference between apiController and controller 好的,因此在阅读了有关apiController和controller之间 stackoverflow 差异的文章之后

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. 我的理解是,如果我要将数据返回到自己的网站,则使用mvc控制器,但是如果我允许第3方使用我网站上的数据,则将数据放入api控制器中。

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. 另外,如果用户访问了我的站点/您的站点并禁用了javascript,那么json就无法在jQuery上正常工作,因为需要jQuery等,因此如果您确定访问者不会禁用javascript,那么我的理解是使用api。

Please let me know if that correct 请让我知道是否正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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