json 使用IEnumerable可视化工具的C# MVC下拉列表

i5desfxk  于 2023-03-31  发布在  C#
关注(0)|答案(1)|浏览(201)

我在IEnumerable Visualizer中有列String。List包含从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);
    }
}

我需要创建一个下拉列表。
currenciesList visualizer
responseFrom visualizer
JSON responseFrom:

[{"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}]}]
baubqpgj

baubqpgj1#

尝试这段代码来创建一个选择列表

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>

和check _ViewImports.cshtml文件,因为您使用的是net.core,所以它应该包含

@using TestCore.WebApp
@using TestCore.WebApp.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

相关问题