Asp.net mvc 4 从控制器更新文本区域

Asp.net mvc 4 从控制器更新文本区域,asp.net-mvc-4,signalr,Asp.net Mvc 4,Signalr,我想在找到一些结果后从控制器更新textarea,页面不应该重新加载。有什么解决办法吗 public class HomeController : Controller { public ActionResult Index() { return View(); } public JsonResult SolveProblems(Problem[] array){ Solution sol=new Solution(array

我想在找到一些结果后从控制器更新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页面。其中包含一些代码和一个文本区域

..... some code....

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

所以,我必须使用信号器并按要求解决我的问题

我需要创建一个集线器,当触发
sol\u OnSolutionFound
时,我可以发送数据

private void sol_OnSolutionFound(object sender, SolutionFoundEventArgs e)
{
    SolutionHub Hub=new SolutionHub();
    Hub.SolutionFound(e.Solution);
}
这是我的中心

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

success
中编写类似
$(“#ResultDisplay”).val(response)
@CarstenLøvboAndersen SolveProblems在主线程下运行,Start将在另一个线程下运行,因此主线程只需启动Start方法,然后就需要时间来找到单个解决方案,所以$(“#ResultDisplay”).val(response)不是解决方案否,不是您当前的编码方式。您需要考虑MVC应用程序的请求-响应。你提出请求,你就会得到回应。您正在尝试:使请求启动backgroundprocess获取请求的响应,然后获取backgroundprocess响应。没有与backgroundprocess响应相匹配的请求,因此它无处可去。您有(至少)两种选择:最简单的方法是通过ajax发出第二个请求,该请求执行后台任务,因为它将在第二个请求下运行,因此不再需要作为后台任务。另一个选择是重构以使用SignalR。在第二个视图中,您已经在为解决问题进行ajax调用-所以不要将其作为后台任务,您可以继续。我明白了-您希望在调用
sol\u OnSolutionFound()
时进行UI更新。SignalR是您的最佳选择,而这个场景正是它的设计初衷。
@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>
}
private void sol_OnSolutionFound(object sender, SolutionFoundEventArgs e)
{
    SolutionHub Hub=new SolutionHub();
    Hub.SolutionFound(e.Solution);
}