MVC 显示集合:InvalidCastException:无法将类型 'Models.ConversionRate' 的对象转换为类型 'System.Collections.IEnumerable'

MVC displaying collection: InvalidCastException: Unable to cast object of type 'Models.ConversionRate' to type 'System.Collections.IEnumerable'

我有一个对象已成功传递到我的视图,其中包含一个正确填充的集合。

我的模特。 conversion_rates 只是双打的集合:

public class Converter
{
    public string result { get; set; }
    public string documentation { get; set; }
    public string terms_of_use { get; set; }
    public string time_zone { get; set; }
    public string time_last_update { get; set; }
    public string time_next_update { get; set; }
    public ConversionRate conversion_rates { get; set; }
}

我的控制器。 currencyConverter 已正确传递到我的视图:

public class Rates
{
    public static Converter Import()
    {
        try
        {
            string URLString = "https://v6.exchangerate-api.com/v6/APIKey/latest/gbp";
            using (var webClient = new System.Net.WebClient())
            {
                var json = webClient.DownloadString(URLString);
                Converter Test = JsonConvert.DeserializeObject<Converter>(json);
                return Test;
            }
        }
        catch (Exception e)
        {
         ...
        }
    }
}


public IActionResult Index()
{
    var currencyConverter = Rates.Import();
    return View(currencyConverter);
}

我的观点:

@using System.Collections;
@model Converter

@foreach (var currency in (IEnumerable)Model.conversion_rates)
{
<tr>
    <td>
        @currency
    </td>
</tr>
}

在调试器中,我可以看到 Model.conversion_rates 已正确填充,但我无法将每个单独的元素存储在我的 var currency 中以便在我的 table.[=18= 中显示]

我可以访问每个单独的元素并通过以下操作很好地显示它们:

**@Successfully pulled data: @Model.conversion_rates.AED**

但出于多种原因,这并不实用。可以动态添加新值等。所以我需要访问我的 Converter.conversion_rates 中的数据,这是我需要访问的集合。

这是我遇到的一个答案,但它给了我以下错误:

InvalidCastException:无法将类型 'Models.ConversionRate' 的对象转换为类型 'System.Collections.IEnumerable'。

最不table,我能找到的类似答案建议将我返回的模型本身转换为 IEnumerable,但它不起作用。我假设这是因为我只返回一个对象,而我要访问的实际集合在 Converter 对象本身内部,因此将 Converter 转换为 1 的集合无济于事。另外,如果我这样做,我也无法直接访问 Model.conversion_rates。

@model IEnumerable<Converter>

@foreach (var currency in Model.conversion_rates)
{
<tr>
    <td>
        @currency
    </td>
</tr>
}

感谢任何建议。

编辑:所以我之前尝试的修复似乎是正确的,我的模型更适合:

public IEnumerable<ConversionRate> conversion_rates { get; set; }

但这弄乱了我的 json 反序列化。如果我找到修复会更新。

最终代码:

修复你的class

public class Converter
{
    ......
    public ConversionRate conversion_rates { get; set; }
    public  IEnumerable<ConversionRateItem> ConversionRateItems { get; set; }
}

public class ConversionRateItem
{
public string Name {get; set;}
public double Value {get; set;}
}

和行动

 var currencyConverter = Rates.Import();

 var items = currencyConverter.conversion_rates.GetType()
        .GetProperties()
        .Select(p => new ConversionRateItem
        {
         Name=p.Name,
        Value = (double) p.GetValue(currencyConverter .conversion_rates, null)
        }).ToArray();

  converter.ConversionRateItems=items;
converter.conversion_rates=null;

    return View(currencyConverter);

最终查看

@foreach (var currency in Model.ConversionRateItems)
{
<tr>
    <td>
        @currency.Name
    </td>
 <td>
        @currency.Value
    </td>
</tr>
}