如何将进度传递到 MVC 页面

发布于 2024-09-26 09:23:21 字数 952 浏览 1 评论 0原文

在 WriteAnObject 上传文件时,我有一个称为定期的委托方法。我想使用 args.PercentDone 值更新 MVC 页面中的 div (ProgressUpdate)。我很感激任何想法? 谢谢,

//委托方法

private void displayProgress(object sender, ProgressArgs args)
{
            //Console.WriteLine(args.PercentDone); //I want to display args.PercentDone in the page
}

//Controller

[HttpPost]
public ActionResult WritingAnObject(MyViewModel bovModel)
{
    //DoSomeStuff which is cause calling displayProgress

    return RedirectToAction("ListingSomeInfo", "testpart");
}

//View

<%using (Html.BeginForm("WritingAnObject", "testpart", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {%>   

    <%:Html.TextBox("catname") %>
    <input type="file" id="fileupload" name="fileupload" />
    <input type="submit" value="Upload" />
    <%} %>


<div id= “ProgressUpdate”</div>

I have a delegate method with is called periodic while WritingAnObject uploading the file. I would like to update div (ProgressUpdate) in my MVC page with args.PercentDone value. I appreciate any idea?
Thanks,

//delegate method

private void displayProgress(object sender, ProgressArgs args)
{
            //Console.WriteLine(args.PercentDone); //I want to display args.PercentDone in the page
}

//Controller

[HttpPost]
public ActionResult WritingAnObject(MyViewModel bovModel)
{
    //DoSomeStuff which is cause calling displayProgress

    return RedirectToAction("ListingSomeInfo", "testpart");
}

//View

<%using (Html.BeginForm("WritingAnObject", "testpart", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {%>   

    <%:Html.TextBox("catname") %>
    <input type="file" id="fileupload" name="fileupload" />
    <input type="submit" value="Upload" />
    <%} %>


<div id= “ProgressUpdate”</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲影 2024-10-03 09:23:21

这是一种在服务器上的操作完成时向用户显示进度的方法。 (需要 javascript)

1) 编写一个在服务器上启动进程的操作。此方法应该更新会话状态中的进度值(以便它特定于用户正在运行的每个会话)。

2) 编写一个客户端可以调用以返回进度的操作。这将读取会话状态中的值。通常,此操作将返回一个小 HTML 片段,其中包含填充正确数量的进度条,或者返回一个包含进度值的 JSON 对象。

3) 从客户端进行 jQuery.ajax() 调用,以在操作运行时异步轮询服务器以获取进度并更新 UI。

附加功能:
- 取消长时间运行的操作的操作
- 在 Web 应用程序外部运行任务(Azure 有一些关于从 Web 应用程序异步运行任务的出色功能)
- 让返回进度的操作也让客户端知道操作是否已完成或取消。

Here is one approach you could take to display progress back to a user while an operaton on the server is completing. (requires javascript)

1) Write an action that starts the process on the server. This method should update a progress value in session state (so that it is specific to each session the user is running).

2) Write an action that the client can call to return progress. This would read the value in session state. Generally this action will return either a small HTML fragment containing the progress bar filled in to the right amount, or a JSON object containing the progress value.

3) From your client, make a jQuery.ajax() call to asynchronously poll the server for progress while the operation is running and update the UI.

Additional bells&whistles:
- an Action to cancel a long running operation
- running the task outside the web application (Azure has some excellent features regarding running tasks asynchronously from a web app)
- Have the action that returns progress also let the client know if the operation is completed or canceled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文