0

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")
8
  • Its not a good idea to call asynchronous code from a view. Commented Jul 12, 2014 at 1:50
  • @DavidG how i can solve this issue? i want that method to run asynchronous Commented Jul 12, 2014 at 2:01
  • Do the asynchronous work inside the method but return synchronously. Commented Jul 12, 2014 at 2:04
  • @DavidG i want to improve application performance thats why i used async method, is there any other way to do this? or how i can do this with this method? Commented Jul 12, 2014 at 2:10
  • 1
    @Usama I'm sorry, but I'm not going to write your code for you. I've given you a nudge in the right direction -- start researching! If you hit any problems, feel free to post another question! Commented Jul 12, 2014 at 2:21

1 Answer 1

4

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:

  • move code to controller and call in from asynchronous top level (non-child) action with await. Pass data to view via model or ViewBag
  • convert to real synchronous code if you must call it from the view or child action
  • if not possible try .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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.