【发布时间】:2015-09-25 19:14:40
【问题描述】:
我看到了一些关于这个东西的问题,但没有一个对我来说很清楚。我有这个:
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>
我正在尝试使用名为 currency 的对象填充 DropDownList,但我不清楚如何实现这一点。
有什么线索吗?
编辑:这正是 curr.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
...
【问题讨论】:
-
您尝试过哪些失败的事情? DropDownList 方法非常简单。
-
@Html.DropDownList("Prueba", Model, ) ?
-
我认为 curr.Get() 没有返回相当于 IEnumerable
selectList
标签: c# asp.net .net asp.net-mvc razor