Use of await in Razor views

前端 未结 8 902
面向向阳花
面向向阳花 2020-11-28 12:48

Is it possible to await on tasks in Razor .cshtml views?

By default it complains that it can only be used in methods marked with async so I

8条回答
  •  孤独总比滥情好
    2020-11-28 13:00

    If you really need it, you can do this, it will be ugly, but it will work.

    In View

    @{  
    var foo = ViewBag.foo;
    var bar = ViewBag.bar;
    }
    

    In Controller

    public async Task Index()
            {
                ViewBag.foo = await _some.getFoo();
                ViewBag.bar = await _some.getBar();
                return View("Index");
            }
    

提交回复
热议问题