显示控制器的进度



我正在构建.NET Core应用程序,我想根据控制器操作的进度更改网站上的文本。我在下面写了一个简短的例子,展示了我想要实现的目标。有些东西是用伪代码编写的。

首页控制器.cs

public class HomeController : Controller
{
private readonly IHomeService _homeService;
public HomeController(IHomeService homeService)
{
_homeService = homeService;
}
public IActionResult Detail(int id)
{
_homeService.ShowInfo(id);
return View(model);
}

首页服务.cs

public void FirstFunction()
{
// write "Fetching results from database"
...
// write "Results fetched successfully"
}
public void SecondFunction()
{
// write "Parsing results"
...
// write "Results parsed successfully"
}
public string ShowInfo(int id)
{
FirstFunction();
SecondFunction();
return "Success!";
}

函数.js

$(document).ready(function() {
$.ajax({
'url' : 'Home/Detail',
'type' : 'GET',
'data' : {
'id' : 123
},
'success' : function(data) {
// update some html element based on "write" commands in
// FirstFunction() and SecondFunction()
}
});
}

如何使用第一函数和第二函数"写入"的字符串更新网页上的 HTML 元素?操作很长,我想为用户提供有关现在正在发生的事情的某种信息。

一种方法是让FirstFunctionSecondFunction设置一些会话变量,然后在客户端使用setinterval触发对服务器的 get 请求(使用 Ajax(,该请求返回会话参数的值。然后向用户显示此内容。

最新更新