首页 > 解决方案 > 在 HTML 中将控制器操作的进度显示为文本

问题描述

我正在构建.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()
        }
    });
}

如何使用 FirstFunction 和 SecondFunctions “写入”的字符串更新网页上的 HTML 元素?这些操作很长,我想给用户一些关于现在正在发生的事情的信息。

标签: javascriptc#asp.netasp.net-mvcasp.net-core

解决方案


推荐阅读