Reputation: 725
I'm trying to return value from async
html helper but it is giving following string instead of desired values.
"System.Threading.Tasks.Task+WhenAllPromise`1[System.Decimal]"
Method:
public async static Task<decimal> CalculateCurrency(this HtmlHelper helper, decimal amount, string from, string country)
{
if (await getValue(country))
{
string fromCurrency = string.IsNullOrEmpty(from) ? "USD" : from;
string toCurrency = country;
WebClient client = new WebClient();
string url = string.Format("http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={0}{1}=X", fromCurrency.ToUpperInvariant(), toCurrency.ToUpperInvariant());
Stream response = await client.OpenReadTaskAsync(url);
StreamReader reader = new StreamReader(response);
string yahooResponse = await reader.ReadLineAsync();
response.Close();
if (!string.IsNullOrWhiteSpace(yahooResponse))
{
string[] values = Regex.Split(yahooResponse, ",");
if (values.Length > 0)
{
decimal rate = System.Convert.ToDecimal(values[1]);
string res = string.Format("{0:0.00}", rate * amount);
return decimal.Parse(res);
}
}
return decimal.Zero;
}
return decimal.Zero;
}
Call HTML Helper:
@Html.CalculateCurrency(22, "USD", "EUR")
Upvotes: 0
Views: 2940
Reputation: 100527
Views don't support asynchronous methods. So as result you get result of default .ToString()
function for type of result which indeed returns just type name.
Options:
await
. Pass data to view via model or ViewBag
.Result
, but watch out for deadlocks. See await vs Task.Wait - Deadlock? for details/links.Note: moving async
code to child action will not help.
Upvotes: 4