简体   繁体   English

在 asp.net 核心 3.1 异步编程中使用带有引导程序 4 的徽章

[英]using badge with bootstrap 4 in asp.net core 3.1 asynchronous programing

I have used badge in asp.net core 3.1 without Async method this is my code in service我在 asp.net 核心 3.1 中使用了徽章,没有异步方法这是我的服务代码

 public int GetNotSeenContact()
    {
        return _context.Contacts.Where(s => s.Seen == false).Count();
    }

and this is call in scope layout:这是 scope 布局中的调用:

 public int GetContactCont()
    {
        return _admin.GetNotSeenContact();
    }

and this is the place that I use this:这是我使用它的地方:

 int ConactBell = scope.GetContactCont();

and this is my html code that I called:这是我调用的 html 代码:

 <span class="badge badge-pill badge-warning notification">@ConactBell</span>

this is working very well but when I make it with Async method, it shows this exception instead of number for user:这工作得很好,但是当我使用 Async 方法制作它时,它会显示这个异常而不是用户的数字:

System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[System.Int32,DIG.Core.Classes.PanelLayoutScope+&lt;GetContactCont&gt;d__3]

Now these are my codes in async method in service:现在这些是我在服务中的异步方法中的代码:

 public async Task<int> GetNotSeenContact()
    {
        return await _context.Contacts.Where(s => s.Seen == false).CountAsync();
    }

this is in my scope class:这是在我的 scope class 中:

 public async Task<int> GetContactCont()
    {
        return await _admin.GetNotSeenContact();
    }

and this is the code where I use it inside html code:这是我在 html 代码中使用它的代码:

@inject PanelLayoutScope scope
@{
Task<int> ConactBell = scope.GetContactCont();}

my html code:我的 html 代码:

<span class="badge badge-pill badge-warning notification">@ConactBell</span>

Please give me the best solution and tell me why does this happen when I use Async method.请给我最好的解决方案,并告诉我为什么在我使用 Async 方法时会发生这种情况。 Thank you so much!太感谢了!

Because the method is asynchronous,you can try to use await.Change因为方法是异步的,可以尝试使用await.Change

@{
Task<int> ConactBell = scope.GetContactCont();}

to

@{
    var ConactBell =await scope.GetContactCont();}

result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM