i saw couple of questions stuff there no 1 clear me. have this:
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) t : new() { using (var w = new webclient()) { var json_data = string.empty; // attempt download json data string try { json_data = w.downloadstring(url); } catch (exception) { } // if string json data not empty, deserialize class , return 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()); } }
and index.cshtml
<div class="form-group"> <label for="fromc">from currency</label> <div class="col-md-6"> @html.dropdownlist("prueba", model, ) </div> </div>
i trying populate dropdownlist object called currencies, don't have clear idea of how implement this.
any clues?
edit: curr.get() returns:
res {worldcurrencies.currencytypes} worldcurrencies.currencytypes aed "united arab emirates dirham" string afn "afghan afghani" string "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 ...
let's take 1 thing time.
what format of data?
it object this:
{ "aed": "united arab emirates dirham", "afn": "afghan afghani", "all": "albanian lek", "amd": "armenian dram", "ang": "netherlands antillean guilder" }
so have bag key/value pairs, key currency's code , value currency's name.
how can deserialize this?
as simple below:
var res = jsonconvert.deserialize<dictionary<string,string>>(json_data);
what want show in our view?
at least dropdown available currencies. if need more data show, have add properties in model , initialize them correspondingly, when create model.
how can achieve this?
@model currencymodel <div class="form-group"> <label for="fromc">from currency</label> <div class="col-md-6"> @html.dropdownlist("currencies", currencymodel.currencies) </div> </div>
what need?
a model @ least 1 property of type ienumerable<selectlistitem>
called currencies
:
public class currencymodel { public ienumerable<selectlistitem> currencies { get; set; } }
who create model ?
this responsibility belongs controller.
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); } }
what needed now create 1 new class 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; } } }
Comments
Post a Comment