2017-08-01 70 views
0

我想從控制器更新textarea後,我發現了一些結果和頁面不應該重新加載。有沒有解決方案?從控制器更新Textarea

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    public JsonResult SolveProblems(Problem[] array){ 

      Solution sol=new Solution(array); 

      sol.OnSolutionFound+=sol_OnSolutionFound; 
      sol.OnTaskComplete+=sol_OnTaskComplete; 

      sol.Start(); 
      return Json("Process started"); 
    } 

    private void sol_OnSolutionFound(object sender, SolutionFoundEventArgs e) 
    { 
     // Here i want update textarea 
    } 

    private void sol_OnTaskComplete(object sender, SolutionCompletedEventArgs e) 
    { 
     // Here i want show process is finished 
    }  
} 

這是我的HTML頁面。其中包含了一些代碼和一個textarea的

..... some code.... 

<textarea class="form-control" id="ResultDisplay"></textarea> 
<button type="button" id="btnSolve" class="btn btn-primary">Solve</button> 

這是我的JavaScript文件

function GetProblems(){ 
    ...code... 
    return array; 
} 

$("#btnSolve").click(function() { 

    $.ajax({ 
     type: "POST", 
     url: "/Home/SolveProblems", 
     contentType: "application/json; charset=utf-8", 
     data: JSON.stringify(GetProblems()), 
     dataType: "json", 

     success: function (response) { 

     }, 
     error: function (response) { 
      alert("Unexpected error occurs!") 
     } 
    }); 
}); 
+0

裏面你'success'寫類似'$(「#ResultDisplay」)。VAL(響應)' –

+0

@CarstenLøvboAndersenSolveProblems被主線程和啓動下運行將在另一個線程運行,因此主線程剛開始啓動方法,之後它需要時間來找到一個單一的解決方案,所以$(「#ResultDisplay」)。val(響應)不是解決方案 – Blue

+0

不,不是你正在編碼的方式。您需要考慮MVC應用程序的請求 - 響應。你提出請求,你會得到迴應。您試圖:請求啓動backgroundprocess - 獲取請求的響應 - *然後*獲取backgroundprocess響應。沒有任何匹配backgroundprocess響應的請求,所以無處可去。你有(至少)兩種選擇:最簡單的方法是通過ajax發出第二個請求來完成後臺任務 - 不再需要後臺任務,因爲它將在第二個請求下運行。另一種選擇是重構使用SignalR。 –

回答

1

所以,我有使用SignalR解決我的問題,因爲freedomn-m告訴我,做

我需要創建通過使用我的Hub可以在sol_OnSolutionFound被觸發時發送數據。

這裏是我的樞紐Index.cshtml

@section scripts { 
<!--Script references. --> 
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. --> 
<!--Reference the SignalR library. --> 
<script src="~/Scripts/jquery.signalR-2.2.2.min.js"></script> 
<!--Reference the autogenerated SignalR hub script. --> 
<script src="~/signalr/hubs"></script> 
<!--SignalR script to update the chat page and send messages.--> 
<script> 
    $(function() { 
     // Reference the auto-generated proxy for the hub. 
     var chat = $.connection.solutionHub; 
     // Create a function that the hub can call back to display messages. 
     chat.client.addNewMessageToPage = function (Solution) { 
      // Add the message to the page. 
      $("#ResultDisplay").append(Solution); 
     }; 

     $.connection.hub.start().done(function() { 
     }); 
    }); 
</script> 
} 

public class SolutionHub : Hub 
{ 
    public void SolutionFound(string Solution) 
    { 
     var hubContext = GlobalHost.ConnectionManager.GetHubContext<SolutionHub>(); 
     hubContext.Clients.All.addNewMessageToPage(Solution); 
    } 
} 

添加部分最後需要調用SolutionFoundsol_OnSolutionFound被激發。

private void sol_OnSolutionFound(object sender, SolutionFoundEventArgs e) 
{ 
    SolutionHub Hub=new SolutionHub(); 
    Hub.SolutionFound(e.Solution); 
}