mvc视图中的异步调用

时间:2014-01-06 15:50:14

标签: c# asp.net-mvc asynchronous

我需要从视图中调用异步方法,但我不知道如何解决这个问题。 我的观点如下:

@using Skipstone.Web.Services;

@{
    var service = new UserService(new CompanyService());
    var users = await service.GetAll();

    ViewBag.Title = "Manage Users";
}

@section Breadcrumb {
    <ol class="breadcrumb">
        <li><a href="/">Home</a></li>
        <li><a href="/Manage/Users">Users</a></li>
    </ol>
}

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="message"></div>

            <a href="/Manage/Users/Create">Create a new user</a>

            <table class="table table-bordered users sortable">
                <thead>
                    <tr>
                        <th data-defaultsort="desc">Username</th>
                        <th>Email</th>
                        <th>Status</th>
                        <th data-defaultsort="disabled">Member of</th>
                        <th data-defaultsort="disabled"></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var user in users)
                    {
                        <tr id="@user.Id">
                            <td>@user.UserName</td>
                            <td>@user.Email</td>
                            <td>
                                @if (user.IsApproved)
                                {
                                    <span class="label label-success label-approved">Approved</span>
                                }
                                else
                                {
                                    <span class="label label-warning label-approved">Rejected</span>
                                }

                                @if (user.IsLockedOut)
                                {
                                    <span class="label label-danger label-locked">Locked</span>
                                }
                            </td>
                            <td>
                                @foreach (var group in user.MemberOf)
                                {
                                    <text>@group </text>
                                }
                            </td>
                            <td>
                                @if (user.IsApproved)
                                {
                                    <a href="#" data-id="@user.Id" class="btn btn-default reject"><span class="glyphicon glyphicon-remove-sign"></span> @*<div class="visible-md visible-lg">Reject</div>*@</a>
                                }
                                else
                                {
                                    <a href="#" data-id="@user.Id" class="btn btn-default approve"><span class="glyphicon glyphicon-ok-sign"></span> @*<div class="visible-md visible-lg">Approve</div>*@</a>
                                }
                                <a href="/Manage/Users/Edit/@user.Id" class="btn btn-default"><span class="glyphicon glyphicon-pencil"></span> @*<div class="visible-md visible-lg">Edit</div>*@</a>
                                <a href="#" data-id="@user.Id" class="btn btn-default delete"><span class="glyphicon glyphicon-remove"></span> @*<div class="visible-md visible-lg">Delete</div>*@</a>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    </div>
</div>

@section Scripts {
    <link href="@Url.Content("~/Content/bootstrap-sortable.css")" rel="stylesheet" />
    <script src="@Url.Content("~/Scripts/bootstrap-sortable.js")" type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/jquery.skipstone-users-index.js")" type="text/javascript"></script>
}

如您所见,该行说明:

  

var users = await service.GetAll();

是一个异步方法,但你不能在不将它们放入异步方法的情况下调用异步方法,所以我有一个恶性循环。

以前有人遇到过这个吗? 我能做些什么让它发挥作用。

注意:我无法访问控制器,因为这是一个虚拟视图,没有控制器,它是从数据库提供的

干杯,

/ r3plica

1 个答案:

答案 0 :(得分:0)

这是在ASP.NET中使用异步调用的不好方法,因为ASP.NET页面本身是异步的,并且不会等到异步操作完成。但是有一些技巧 - 阅读Scott Hanselman的帖子here如果没有其他选项存在,我建议通过调用RunSynchronously()

来同步运行它