等待在ASP.NET网页的Razor视图任务 [英] Waiting on tasks in ASP.NET WebPages Razor view

查看:93
本文介绍了等待在ASP.NET网页的Razor视图任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用异步/在等待着,呼唤从我的观点之一的异步方法,我需要等待,直到它完成。 我已经看到了很多的ASP例子.NET MVC,在那里你可以把一个异步到你的行动。签名但我还没有看到ASP.NET网页的例子。

我只是叫我的Razor视图内返回的任务等待()?我见过对等待的建议()。

请给我一些参考有关如何正确地调用异步方法从网页剃刀视图中/例子。


解决方案

  

这是我的一个视图调用异步方法


不要从的视图中调用方法的。认为确实应该仅仅是结合这是对模型的数据。如果有要执行异步操作来获取模型的数据,在控制器填充模型时执行它

作为一个人为的例子:

 公共类MyViewModel
{
    公共字符串someValue中获得{;组; }
}

和在控制器:

  VAR模型=新MyViewModel
{
    someValue中等待= GetTheValue();
};
返回查看(模型);

和视图:

  @ Model.SomeValue

当它给视图来呈现,并认为应该仅仅是使得它的模型主要应完全。异步操作应该是在控制器中。


作为替代把太多code在控制器和保持它在模型中,你可以移动一个异步操作以在模型上某种初始化:

 公共类MyViewModel
{
    公共字符串someValue中获得{;私人集; }    公共异步任务初始化()
    {
        someValue中等待= GetTheValue();
    }
}

然后在您的控制器你调用一个:

  VAR模型=新MyViewModel();
等待model.Initialize();
返回查看(模型);


或许异步工厂型号:

 公共类MyViewModel
{
    公共字符串someValue中获得{;私人集; }    私人MyViewModel(){}    公共静态异步MyViewModel的Create()
    {
        返回新MyViewModel
        {
            someValue中等待= GetTheValue()
        };
    }
}

然后在控制器:

  VAR模型=等待MyViewModel.Create();
返回查看(模型);

有多种方法去了解这个,真的。最主要的是要保持异步操作出来的观点,其唯一的工作应该是渲染UI一旦模型完成的。

I am using async/await, and calling an async method from one of my views and I need to wait until it finishes. I've seen a lot of examples for ASP.NET MVC, where you can just put an "async" into the signature of your action. But I haven't seen any examples for ASP.NET WebPages.

Do I just call "Wait()" on the returned task inside my Razor view? I've seen recommendations against Wait().

Please give me some references/examples on how to properly call async methods from within a Razor view in WebPages.

解决方案

calling an async method from one of my views

Don't call methods from within the view. The view should really just be binding to data that's on the model. If there's an asynchronous operation to be performed to fetch the model's data, perform it in the controller when populating the model.

As a contrived example:

public class MyViewModel
{
    public string SomeValue { get; set; }
}

and in the controller:

var model = new MyViewModel
{
    SomeValue = await GetTheValue();
};
return View(model);

and in the view:

@Model.SomeValue

The model should essentially be "complete" when it's given to the view to render, and the view should just be rendering it. The asynchronous operations should be in the controller.


As an alternative to putting too much code in the controller and keeping it in the model, you can move an asynchronous operation to some kind of initialization on the model:

public class MyViewModel
{
    public string SomeValue { get; private set; }

    public async Task Initialize()
    {
        SomeValue = await GetTheValue();
    }
}

Then in your controller you'd invoke that:

var model = new MyViewModel();
await model.Initialize();
return View(model);


Or perhaps an asynchronous factory on the model:

public class MyViewModel
{
    public string SomeValue { get; private set; }

    private MyViewModel() { }

    public static async MyViewModel Create()
    {
        return new MyViewModel
        {
            SomeValue = await GetTheValue()
        };
    }
}

Then in the controller:

var model = await MyViewModel.Create();
return View(model);

There are a number of ways to go about this, really. The main thing is to keep the asynchronous operations out of the view, whose only job should be to render the UI once the model is complete.

这篇关于等待在ASP.NET网页的Razor视图任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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