[asp.net-mvc]填充与传递从控制器 C# 对象的Razor下拉列表

发布时间: 2017/3/25 0:37:42
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

看到了几个问题,这件事,但没有人清楚对我来说。我有这个︰

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>

我试图填充下拉列表中的对象被称为货币,但是我没有清楚地知道如何实现这一点。

任何的线索吗?

编辑︰这是到底什么课程。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
    ...

解决方法 1:

让我们看这一件事一段时间。

您的数据的格式是什么?

它是这样的对象︰

{
    "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;
        }
    }
}
官方微信
官方QQ群
31647020