I have a vb.net MVC3 application using RAZOR views. Some of the functions take a long time to complete due to the fact that they are compiling data from the database. During this long wait the browser screen is white and there is no sign that it is doing whats its supposed to other than the activity ring. What I would like to do is create a progress bar, use some simple math on the count method to determine the progress bar percent complete and show this on the screen instead of the plain white browser screen.. I am certain that I would need to do this with a Jquery widget of some sort. And have looked all over google but nothing seems to be hitting on what I am needing here.. The controller function that is called when the White screen is displayed until it is finished is below:
我有一个使用RAZOR视图的vb.net MVC3应用程序。有些函数需要很长时间才能完成,因为它们正在从数据库中编译数据。在这漫长的等待过程中,浏览器屏幕是白色的,没有任何迹象表明它在做它应该做的事情,而不是活动圈。我想做的是创建一个进度条,使用一些简单的计数方法来确定进度条完成的百分比,并在屏幕上显示这个,而不是在纯白色的浏览器屏幕上。我确信我需要使用某种Jquery小部件来实现这一点。我在谷歌上找了很多遍,但似乎没有什么能满足我的需要。当白色屏幕显示到完成时,调用的控制器函数如下:
Function beginArchive()
Dim cList As List(Of cours) = db.courses.ToList
For Each x In cList
Dim indCourse = x
Dim courHfile As String = archiveFiles(x.course_id)
Dim arcCourse As New archive
arcCourse.cDesc = indCourse.course_description
If Not String.IsNullOrEmpty(courHfile) Then
arcCourse.cHandouts = courHfile
End If
arcCourse.cHours = indCourse.course_hours
arcCourse.conNum = Convert.ToInt16(indCourse.courseNum)
arcCourse.cPre = indCourse.course_prefix
arcCourse.cRef = indCourse.course_ref
arcCourse.cSpeaker = indCourse.course_speaker
arcCourse.cTitle = indCourse.course_title
db.archives.AddObject(arcCourse)
db.SaveChanges()
Dim testSuccess As Integer = delOldFiles(indCourse.course_id)
Next
Return RedirectToAction("Index", "Admin")
End Function
Razor View is this:
剃须刀的观点是这样的:
@Code
ViewData("Title") = "Archive Previous Conf. Handouts"
End Code
<fieldset>
<h2>You are about to archive all handouts from last years conference. If you continue all of those handouts will be moved to the archive</h2>
<div id="sidebar3">
<p>
@Html.ActionLink("Begin Archive", "beginArchive","handoutArchive")
@Html.ActionLink("Back to Admin Tools", "Index", "Admin")
</p>
</div>
</fieldset>
Any ideas on how I would go about adding a simple progress bar to this which I am assuming would have to be fired by the controller itself...
任何关于我如何添加一个简单的进度条的想法,我假设它将会被控制器触发……
2 个解决方案
#1
2
What you are essentially asking for cannot be achieved with the current design of your page/actions. You are clicking a link that makes a request to your controller. The controller is going to wait until the action is finished before rendering and returning a response. This is different than making an ajax call which would return the data to the current page instead of directing you to a new url. There are many ways to do this, and a lot of them depend on your requirements for how you want it to work.
你本质上要求的是不能用当前页面/动作的设计来实现。您正在单击向控制器发出请求的链接。控制器将等待直到操作完成后才呈现并返回响应。这与使用ajax调用将数据返回到当前页面而不是指向新的url不同。有很多方法可以做到这一点,其中许多方法取决于您希望它如何工作的需求。
If it was me, and I was trying to do a large operation, This is what I would do. Display a spinner or some sort of icon with a message saying "this could take awhile" and make an ajax request to your action. When the action returns you can display a message or redirect to a new route.
如果是我,我想做一个大手术,这就是我要做的。显示一个转轮或某种图标,并显示一条消息“这可能需要一段时间”,并向您的操作发出ajax请求。当操作返回时,您可以显示消息或重定向到新的路由。
An example using jquery
一个例子使用jquery
$.post('beingArchive/2012', function(data) {
if(data.status === "success"){
window.location.replace("/success");
}else{
$("#error-div").html("<em>Something went wrong!</em>");
}
});
This assumes that your beginArchive
method returns json like {"status":"success"}
or {"status":"error archiving reason blah"}
这假定你的beginArchive方法返回json,比如{"status":"success"}或{"status":"error archiving blah"}
[HttpPost]
Public Function beginArchive() As JsonResult
Dim status = "success"
Try
'do the archiving
Catch e As Exception
status = "error occurred"
End Try
Return New Json(New With { _
Key .status = status _
})
End Function
(I'm a c# guy so apologies if the vb.net is not right, it's just an example of how I envision the implementation of the action). Unfortunately, without knowing more about what your implementation of "archiving handouts" is, I can't really tell you a good solution for showing the progress of the action would be. But I feel like this is a standard solution that many applications use when processing intensive operations.
(我是c#人员,如果vb.net不正确,我很抱歉,这只是我如何设想行动的实现的一个例子)。不幸的是,如果不了解“存档讲义”的实现方式,我就无法真正告诉您一个好的解决方案,以显示行动的进展。但我觉得这是一个标准的解决方案,许多应用程序在处理密集型操作时使用。
#2
0
Your html code would be good to see here for a better response, but what you could do easily enough is have your process run in an ajax.beginform, and use the onbegin/oncomplete parameters of the begin form to call a startProgressbar / stopProgressbar method. Just have an animated gif in a div, and make those 2 methods above do a show/hide for that div.
您的html代码可以在这里看到更好的响应,但是您可以很容易地做到的是让您的流程在ajax中运行。开始,并使用开始表单的onbegin/oncomplete参数来调用startProgressbar / stopProgressbar方法。只要在一个div中有一个gif动画,并使上面的两个方法为那个div做一个显示/隐藏。
#1
2
What you are essentially asking for cannot be achieved with the current design of your page/actions. You are clicking a link that makes a request to your controller. The controller is going to wait until the action is finished before rendering and returning a response. This is different than making an ajax call which would return the data to the current page instead of directing you to a new url. There are many ways to do this, and a lot of them depend on your requirements for how you want it to work.
你本质上要求的是不能用当前页面/动作的设计来实现。您正在单击向控制器发出请求的链接。控制器将等待直到操作完成后才呈现并返回响应。这与使用ajax调用将数据返回到当前页面而不是指向新的url不同。有很多方法可以做到这一点,其中许多方法取决于您希望它如何工作的需求。
If it was me, and I was trying to do a large operation, This is what I would do. Display a spinner or some sort of icon with a message saying "this could take awhile" and make an ajax request to your action. When the action returns you can display a message or redirect to a new route.
如果是我,我想做一个大手术,这就是我要做的。显示一个转轮或某种图标,并显示一条消息“这可能需要一段时间”,并向您的操作发出ajax请求。当操作返回时,您可以显示消息或重定向到新的路由。
An example using jquery
一个例子使用jquery
$.post('beingArchive/2012', function(data) {
if(data.status === "success"){
window.location.replace("/success");
}else{
$("#error-div").html("<em>Something went wrong!</em>");
}
});
This assumes that your beginArchive
method returns json like {"status":"success"}
or {"status":"error archiving reason blah"}
这假定你的beginArchive方法返回json,比如{"status":"success"}或{"status":"error archiving blah"}
[HttpPost]
Public Function beginArchive() As JsonResult
Dim status = "success"
Try
'do the archiving
Catch e As Exception
status = "error occurred"
End Try
Return New Json(New With { _
Key .status = status _
})
End Function
(I'm a c# guy so apologies if the vb.net is not right, it's just an example of how I envision the implementation of the action). Unfortunately, without knowing more about what your implementation of "archiving handouts" is, I can't really tell you a good solution for showing the progress of the action would be. But I feel like this is a standard solution that many applications use when processing intensive operations.
(我是c#人员,如果vb.net不正确,我很抱歉,这只是我如何设想行动的实现的一个例子)。不幸的是,如果不了解“存档讲义”的实现方式,我就无法真正告诉您一个好的解决方案,以显示行动的进展。但我觉得这是一个标准的解决方案,许多应用程序在处理密集型操作时使用。
#2
0
Your html code would be good to see here for a better response, but what you could do easily enough is have your process run in an ajax.beginform, and use the onbegin/oncomplete parameters of the begin form to call a startProgressbar / stopProgressbar method. Just have an animated gif in a div, and make those 2 methods above do a show/hide for that div.
您的html代码可以在这里看到更好的响应,但是您可以很容易地做到的是让您的流程在ajax中运行。开始,并使用开始表单的onbegin/oncomplete参数来调用startProgressbar / stopProgressbar方法。只要在一个div中有一个gif动画,并使上面的两个方法为那个div做一个显示/隐藏。