如何在完成渲染之前加载剃须刀页面?
我在index.cshtml
中有一个记录表,一旦用户单击记录以查看将它们带到lidets.cshtml
的详细信息。
问题是,有很多数据正在提取,并且页面的响应时间较慢。我想添加一个加载旋转器,但页面没有切换,它只是在index.cshtml上保留,直到所有数据详细加载所有数据。
我如何使我的应用程序跳至详细信息。cshtml用户单击记录,进行加载旋转器显示,然后完成加载页面。我不希望用户坐在索引页面上等待。
index.cshtml
<a asp-page="/Details" asp-route-id="@Person.id">Select</a>
详细信息。cshtml.cs,
//simplified
public async Task<IActionResult> OnGetAsync(int? id)
{
var data = await _dataService.GetData(id);
return Page();
}
I have a table of records in Index.cshtml
, once a user clicks on a record to view the details it will take them to Details.cshtml
.
The issue is, there is a lot of data being pulled and the page has a slow response time. I would like to add a loading spinner but the page doesn't switch, it just stays on Index.cshtml until all the data is loaded from the OnGet in Details.cshtml then it switches the page fully loaded/rendered.
How do I get my application to jump to Details.cshtml once a user clicks a record, have my loading spinner display, and then finish loading page. I don't want the user to be sitting waiting at the Index page.
Index.cshtml
<a asp-page="/Details" asp-route-id="@Person.id">Select</a>
Details.cshtml.cs,
//simplified
public async Task<IActionResult> OnGetAsync(int? id)
{
var data = await _dataService.GetData(id);
return Page();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将以以下方式进行。
步骤1:您确定您的数据访问代码是否已优化?
在这里可以做什么:
SELECT *从Mytable
中进行选择,并最终加载他们在mytable
中定义的所有35个字段,而他们实际上需要2或3个字段。步骤2:您是否真的需要向用户显示所有整个结果?
有时我们创建网页,其中我们向用户显示1000个数据记录。大多数时候,这是没有用的:您导航到Google搜索结果的第二页多少次?我的意思是,您可以考虑仅向用户展示前10个项目,并为他们提供更多负载按钮,分页系统或过滤搜索以减少结果集的方法。
步骤3:您是否正在进行很多加入来加载数据?
也许您需要准备一个读取模型,该模型正好是1:1,其中需要显示的数据,以便在您的视图中显示您可以在其上运行简单而简单的查询(根本没有加入),并且可以快速简单地获取数据。考虑探索CQRS架构模式。
步骤4:您可以评估以加载数据客户端,以便
快速向用户显示一些东西,并让实际数据懒洋洋地加载用户浏览器。
为此,您需要使用AJAX请求和一些JavaScript将大量页面延长移至客户端。
如果您的所有应用程序都是渲染的服务器端,并且您正在从剃须刀页面或MVC项目中工作,我将避免这样做。这是一个完全不同的范式,无论如何,您需要优化数据访问代码并查看要为用户加载的数据量。
在我看来,在大多数情况下,步骤1和步骤2就足够了。
I would proceed in the following manner.
Step 1: are you sure that your data access code is optimized ?
What can you do here:
SELECT * FROM MyTable
and end up loading all the 35 fields they have defined inMyTable
when they actually need 2 or 3 of them.Step 2: do you really need to show all the entire result set to the user ?
Sometimes we create web pages where we show 1000 record of data to the user. Most of the times this is useless: how many times you navigated to the second page of results of a Google search ? I mean that you can consider to show your users only the first 10 items and offer them a load more button, or a pagination system or a way to filter their search in order to reduce the result set.
Step 3: are you doing a lot of joins to load your data ?
Maybe you need to prepare a read model which is exactly 1:1 with the data that you need to show in your view, so that you can run a plain and simple query on it (no joins at all) and you are able to fetch the data in a quick and simple way. Consider exploring the CQRS architectural pattern.
Step 4: you can evaluate to load the data client side, in order to
quickly show something to the user and let the actual data to load lazily, in the user browser.
To do this, you need to move a great amount of your page rending to the client, by using an AJAX request and some Javascript.
If all of your application is rendered server side and you are working from a Razor Page or an MVC project, I would avoid to do this. It's a complete different paradigm and, in any case, you need to optimize your data access code and review the amount of data that you want to load for the user.
In my opinion Step 1 and Step 2 are enough in the majority of cases.
您可以尝试使用部分页面和JS,使用JS调用部分页面,然后将ID传递给其,然后使用
等待_dataservice.getData(ID)(ID);
:deletss.cshtml:
详细信息。 cshtml.cs:
index2.cshtml.cs:
更新:
如果您不想返回布局元素,请尝试将以下代码添加到index2.cshtml:
You can try to use a partial page and js,use js to call the partial page,and pass the id to it,then use
await _dataService.GetData(id);
:Details.cshtml:
Details.cshtml.cs:
Index2.cshtml.cs:
Update:
If you don't want to return the layout elements,try to add the following code to Index2.cshtml: