2012-08-06 38 views
2

我有一個使用RAZOR視圖的vb.net MVC3應用程序。一些功能需要很長時間才能完成,因爲它們正在編譯數據庫中的數據。在這漫長的等待期間,瀏覽器屏幕是白色的,並且沒有跡象表明它正在做什麼而不是活動環。我想要做的是創建一個進度條,在count方法上使用一些簡單的數學來確定進度條完成百分比,並在屏幕上顯示它,而不是純白色的瀏覽器屏幕。我確信我需要使用某種類型的Jquery小部件來做到這一點。而且到處都找過谷歌,但似乎沒有任何要什麼我需要在這裏打..當顯示白屏,直到它完成時調用的控制器功能低於:如何從控制器創建進度條以處理MVC3中的長進程

 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視圖是這個:

@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> 

如何,我會去加入一個簡單的進度條,這裏面我假設都必須由控制器本身被解僱任何想法...

回答

2

您實質上要求的內容不能用當前的頁面/動作設計來實現。您正在點擊向您的控制器發出請求的鏈接。控制器將在渲染並返回響應之前等待動作完成。這與創建一個將數據返回到當前頁面的ajax調用不同,而不是將您指向新的url。有很多方法可以做到這一點,其中很多方法取決於您的要求,以確保您的工作方式。

如果是我,我正在嘗試做一個大型的手術,這是我會做的。 顯示一個微調或某種圖標,並顯示「這可能需要一段時間」的消息,並向您的操作發出ajax請求。當動作返回時,您可以顯示消息或重定向到新路線。

一個例子使用jQuery

$.post('beingArchive/2012', function(data) { 
    if(data.status === "success"){ 
     window.location.replace("/success"); 
    }else{ 
     $("#error-div").html("<em>Something went wrong!</em>"); 
    } 
}); 

這是假設你的beginArchive方法返回JSON像{"status":"success"}{"status":"error archiving reason 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 

(我是一個C#的傢伙所以道歉,如果vb.net不對,這只是我設想實施該行動的一個例子)。 不幸的是,在不瞭解你的「歸檔講義」實施的情況下,我無法告訴你一個很好的解決方案來展示行動的進展。但我覺得這是許多應用程序在處理密集型操作時使用的標準解決方案。

+0

我可以只是有beginArchive返回成功,而不是使用重定向? – Skindeep2366 2012-08-06 02:25:15

+0

你可以在jQuery post調用的函數回調中做任何你想做的事情。如果你想顯示一條消息,並在成功時保持在同一頁面上,只需將'window.location'行替換爲其他內容即可。 – 2012-08-06 02:28:02

+0

對不起,如果我假設jQuery/JavaScript知識作爲我的答案的一部分。但據我所知,這是處理這個問題的好方法。 – 2012-08-06 02:29:16

0

你的HTML代碼將是一件好事在這裏看到更好的迴應,但你可以輕鬆地做什麼e nough是讓你的進程在ajax.beginform中運行,並使用begin表單的onbegin/oncomplete參數來調用startProgressbar/stopProgressbar方法。只需在div中添加一個動畫gif,並使上述2種方法對該div進行顯示/隱藏。

+0

實際上沒有與此相關的HTML代碼,因爲它完成後返回到調用索引 – Skindeep2366 2012-08-06 01:50:33

+0

必須有一個初始的HTML代碼,對吧?您想用來實際顯示此進度欄的頁面?什麼調用上面的這個方法,然後按下按鈕?計時器? – Phil 2012-08-06 01:57:29

+0

編輯包括調用視圖... – Skindeep2366 2012-08-06 02:05:01