2017-08-01 75 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); 
}