C# 使用从控制器C传递的对象填充Razor DropDownList#

C# 使用从控制器C传递的对象填充Razor DropDownList#,c#,asp.net,.net,asp.net-mvc,razor,C#,Asp.net,.net,Asp.net Mvc,Razor,我看到了一些关于这件事的问题,但没有一个是我清楚的。我有这个: 货币 public class Currencies { public WorldCurrencies.CurrencyTypes Get() { var url = "http://openexchangerates.org/api/currencies.json"; var currencies = _download_serialized_json_data<Currenc

我看到了一些关于这件事的问题,但没有一个是我清楚的。我有这个:

货币

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;
        }
    }
}
和Index.cshtml

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

让我们一次只做一件事

您的数据格式是什么

它是这样一个对象:

{
    "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);
谁将创建此模型

这项责任属于财务总监

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);
    }
}
现在需要的是创建一个新类
currencesrepository

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;
        }
    }
}
public class currencesrepository
{
字符串url=”http://openexchangerates.org/api/currencies.json";
公共IDictionary GetAll()
{
使用(var webClient=new webClient())
{
var data=webClient.DownloadString(_url);
var currences=JsonConvert.DeserializeObject(数据);
返回货币;
}
}
}

您尝试了什么但失败了?这个方法非常简单。@Html.DropDownList(“Prueba”,Model,)?我认为curr.Get()不会返回与IEnumerable SelectList等价的值,我需要送你一杯啤酒或一些东西来感谢你。所以再次感谢你对假人的解释。我必须在一个月的时间里在我的工作场所学习C#,现在很难实现所有东西。。。非常感谢,伙计。@Tincho.UY非常欢迎你!我很高兴我帮了忙:)
@model CurrencyModel

<div class="form-group">
    <label for="fromC">From Currency</label>
    <div class="col-md-6">
        @Html.DropDownList("Currencies", CurrencyModel.Currencies)
    </div>
</div>
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);
    }
}
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;
        }
    }
}