使用 IEnumerable Visualizer 的 C# MVC 下拉列表

问题描述 投票:0回答:1

我在 IEnumerable Visualizer 中有名为“String”的列。列表包含从 API 下载的元素(THB、USD、EUR 等...)。如何在视图中为此列创建下拉列表。

我在尝试:

@Html.Dropdownlist("String", ViewBag.currenciesList as SelectList, "Select Value", new {@class ="form-control"});

它不起作用。在“字符串”中显示异常。

代码如下:

[HttpPost]
        public ActionResult ConvertCurrencies(decimal amount, string currencyCode, string fromCurrency, string toCurrency)
        {
            var nbpApiUrl = RatesExchangeServices.tableAUrl;


            using (var client = new HttpClient())
            {
                //Exchange rate from currency :
                client.BaseAddress = new Uri(nbpApiUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage getdataFrom = client.GetAsync(nbpApiUrl).Result;
                string responseFrom = getdataFrom.Content.ReadAsStringAsync().Result;
                if (!getdataFrom.IsSuccessStatusCode)
                {
                    throw new Exception("Valid connection with Api");
                }
                dynamic jsonResponseFrom = JsonConvert.DeserializeObject(responseFrom);

                var currenciestList = new List<string>();

                foreach (var rate in jsonResponseFrom[0].rates)
                {
                    currenciestList.Add(rate.code.ToString());
                }
                ViewBag.currenciesList = new SelectList(currenciestList);
 
 I need to create dropdownlist.

currenciesList visualizer

responseFrom visualizer

json reponseFrom :

[{"table":"A","no":"055/A/NBP/2023","effectiveDate":"2023-03-20","rates":[{"currency":"bat (Tajlandia)","code":"THB","mid":0.1294},{"currency":"dolar amerykański","code":"USD","mid":4.4130},{"currency":"dolar australijski","code":"AUD","mid":2.9466},{"currency":"dolar Hongkongu","code":"HKD","mid":0.5626},{"currency":"dolar kanadyjski","code":"CAD","mid":3.2168},{"currency":"dolar nowozelandzki","code":"NZD","mid":2.7521},{"currency":"dolar singapurski","code":"SGD","mid":3.2890},{"currency":"euro","code":"EUR","mid":4.7109},{"currency":"forint (Węgry)","code":"HUF","mid":0.011809},{"currency":"frank szwajcarski","code":"CHF","mid":4.7477},{"currency":"funt szterling","code":"GBP","mid":5.3808},{"currency":"hrywna (Ukraina)","code":"UAH","mid":0.1258},{"currency":"jen (Japonia)","code":"JPY","mid":0.033634},{"currency":"korona czeska","code":"CZK","mid":0.1963},{"currency":"korona duńska","code":"DKK","mid":0.6328},{"currency":"korona islandzka","code":"ISK","mid":0.031469},{"currency":"korona norweska","code":"NOK","mid":0.4119},{"currency":"korona szwedzka","code":"SEK","mid":0.4205},{"currency":"lej rumuński","code":"RON","mid":0.9572},{"currency":"lew (Bułgaria)","code":"BGN","mid":2.4086},{"currency":"lira turecka","code":"TRY","mid":0.2322},{"currency":"nowy izraelski szekel","code":"ILS","mid":1.2003},{"currency":"peso chilijskie","code":"CLP","mid":0.005336},{"currency":"peso filipińskie","code":"PHP","mid":0.0809},{"currency":"peso meksykańskie","code":"MXN","mid":0.2321},{"currency":"rand (Republika Południowej Afryki)","code":"ZAR","mid":0.2384},{"currency":"real (Brazylia)","code":"BRL","mid":0.8365},{"currency":"ringgit (Malezja)","code":"MYR","mid":0.9837},{"currency":"rupia indonezyjska","code":"IDR","mid":0.00028731},{"currency":"rupia indyjska","code":"INR","mid":0.053404},{"currency":"won południowokoreański","code":"KRW","mid":0.003367},{"currency":"yuan renminbi (Chiny)","code":"CNY","mid":0.6408},{"currency":"SDR (MFW)","code":"XDR","mid":5.9184}]}]
c# json drop-down-menu viewbag selectlist
1个回答
0
投票

尝试使用此代码创建一个选择列表

string json = getdataFrom.Content.ReadAsStringAsync().Result;

 var rates = ((JArray)JArray.Parse(json)[0]["rates"])
                .Select(v => new SelectListItem { Text = (string)v["code"], Value = v["code"].ToString() });

 ViewBag.currenciesList = rates;

和视图

 <select  asp-for="... your field" asp-items="ViewBag.currenciesList"></select>
© www.soinside.com 2019 - 2024. All rights reserved.