Razor视图中的异步方法调用 [英] Async method call in Razor view

查看:399
本文介绍了Razor视图中的异步方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有@ Html.Translate()扩展方法,该方法返回翻译后的字符串.翻译存储在数据库中,因此我想从助手方法对数据库进行异步调用. 我可以从razor视图进行异步方法调用吗? 这里有一些变通方法来在razor视图中调用异步方法吗?

I have @Html.Translate() extension method, which returns translated string. Translations are stored in database, so I want to make async call to database from helper method. Can I make async method calls from razor view? Is here some workaround to call async methods in razor view?

推荐答案

仅在ASP.NET vNext(MVC 6)中支持(或将支持)从剃刀视图进行异步方法调用.

Async method calls from razor view are supported (or will be supported) only in ASP.NET vNext (MVC 6).

如果您使用的是MVC 5及更高版本,则最简单的解决方案是在视图模型中添加属性,并通过对db的异步调用填充该属性.因此,在呈现视图时,所有数据将已经在模型中并可以使用.

If you are using MVC 5 version and eralier, the simplest solution to your problem would be adding a property to your view model and filling it with async call to db. So when your view is rendered all the data will already be in the model and ready for use.

PS 也从视图中调用数据库听起来有点麻烦.也许您应该查看这种模式.

P.S also calling a database from view sounds a little fishy. May be you should review this pattern.

这篇关于Razor视图中的异步方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆