cannot return value from async method in view

前端 未结 1 1931
北海茫月
北海茫月 2021-01-27 15:52

I\'m trying to return value from async html helper but it is giving following string instead of desired values.

\"System.Threading.Tasks.Tas

相关标签:
1条回答
  • 2021-01-27 16:39

    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.

    0 讨论(0)
提交回复
热议问题