用c#控制器传递的对象填充Razor下拉列表

本文关键字:对象 填充 Razor 下拉列表 控制器 | 更新日期: 2023-09-27 18:14:37

我看到了关于这个东西的几个问题,但没有一个清楚的我。我有这个:

Currencies.cs

public class Currencies
{
    public WorldCurrencies.CurrencyTypes Get()
    {
        var url = "http://openexchangerates.org/api/currencies.json";
        var currencies = _download_serialized_json_data<CurrencyTypes>(url);
        return currencies;
    }
    private static T _download_serialized_json_data<T>(string url) where T : new()
    {
        using (var w = new WebClient())
        {
            var json_data = string.Empty;
            // attempt to download JSON data as a string
            try
            {
                json_data = w.DownloadString(url);
            }
            catch (Exception) { }
            // if string with JSON data is not empty, deserialize it to class and return its instance 
            var res = !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject<T>(json_data) : new T();
            return res;
        }
    }
}

WebAPIController.cs

public class WebAPIController : Controller
{
    public ActionResult Converter()
    {
        WorldCurrencies.Currencies curr = new WorldCurrencies.Currencies();
        return View("Index", curr.Get());
    }
}

和Index.cshtml

<div class="form-group">
    <label for="fromC">From Currency</label>
    <div class="col-md-6">
        @Html.DropDownList("Prueba", Model, )
    </div>
</div>

我正在尝试用名为货币的对象填充下拉列表,但我不清楚如何实现这一点。

什么线索吗?

EDIT:这就是current . get()返回的结果:

    res {WorldCurrencies.CurrencyTypes} WorldCurrencies.CurrencyTypes
    AED "United Arab Emirates Dirham"   string
    AFN "Afghan Afghani"    string
    ALL "Albanian Lek"  string
    AMD "Armenian Dram" string
    ANG "Netherlands Antillean Guilder" string
    AOA "Angolan Kwanza"    string
    ARS "Argentine Peso"    string
    AUD "Australian Dollar" string
    AWG "Aruban Florin" string
    AZN "Azerbaijani Manat" string
    BAM "Bosnia-Herzegovina Convertible Mark"   string
    BBD "Barbadian Dollar"  string
    BDT "Bangladeshi Taka"  string
    BGN "Bulgarian Lev" string
    ...

用c#控制器传递的对象填充Razor下拉列表

让我们一次做一件事。

你的数据格式是什么?

它是一个像这样的对象:

{
    "AED": "United Arab Emirates Dirham",
    "AFN": "Afghan Afghani",
    "ALL": "Albanian Lek",
    "AMD": "Armenian Dram",
    "ANG": "Netherlands Antillean Guilder"
}

我们有一个包含键/值对的包,键是货币的代码,值是货币的名称。

我们如何对其进行反序列化?

如下所示:

var res = JsonConvert.Deserialize<Dictionary<string,string>>(json_data);

我们想在视图中显示什么?

至少有一个包含所有可用货币的下拉列表。如果需要显示更多的数据,则必须在模型中添加额外的属性,并在创建模型时对其进行相应的初始化。

我们如何做到这一点?

@model CurrencyModel
<div class="form-group">
    <label for="fromC">From Currency</label>
    <div class="col-md-6">
        @Html.DropDownList("Currencies", CurrencyModel.Currencies)
    </div>
</div>

我们需要什么?

具有至少一个IEnumerable<SelectListItem>类型属性的模型,称为Currencies:

public class CurrencyModel
{
    public IEnumerable<SelectListItem> Currencies { get; set; }
}

谁来创建这个模型?

这个责任属于控制器。

public class WebAPIController : Controller
{
    public ActionResult Converter()
    {
        var currencies = new CurrenciesRepository.GetAll();
        var currencyModel = new CurrencyModel
        {
            Currencies =
                currencies.Select(currency => new SelectListItem {
                                                  Text = currency.Value, 
                                                   Value = currency.Key});
        }
        return View("Index", currencyModel);
    }
}

现在需要的是创建一个新的类CurrenciesRepository

public class CurrenciesRepository
{
    string _url = "http://openexchangerates.org/api/currencies.json";
    public IDictionary<string, string> GetAll()
    {
        using (var webClient = new WebClient())
        {
            var data = webClient.DownloadString(_url);
            var currencies= JsonConvert.DeserializeObject<Dictionary<string,string>>(data);
            return currencies;
        }
    }
}