【问题标题】:Update Textarea from controller从控制器更新 Textarea
【发布时间】:2017-08-04 11:26:55
【问题描述】:

在我找到一些结果并且页面不应该重新加载后,我想从控制器更新 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>

这是我的 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!")
        }
    });
 });

【问题讨论】:

  • 在你的 success 里面写 $("#ResultDisplay").val(response) 之类的东西
  • @CarstenLøvboAndersen SolveProblems 在主线程下运行,而 Start 将在另一个线程下运行,因此主线程只需启动 Start 方法,然后需要时间来找到单个解决方案,因此 $("#ResultDisplay"). val(response) 不是解决方案
  • 不,不是您当前编码的方式。您需要考虑 MVC 应用程序的请求-响应。你提出请求,你就会得到回应。您正在尝试:制作请求-启动后台进程-获取请求的响应-然后获取后台进程响应。没有与后台进程响应匹配的请求,因此无处可去。您有(至少)两个选择:最简单的方法是通过执行后台任务的 ajax 发出第二个请求 - 它不再需要是后台任务,因为它将在第二个请求下运行。另一种选择是重构以使用 SignalR。
  • 在第二个视图中,您已经在为 SolveProblems 进行 ajax 调用 - 所以不要将其设为后台任务,您就可以开始了。
  • 我明白了——无论何时调用 sol_OnSolutionFound(),您都需要更新 UI。 SignalR 是您最好的选择,这种情况几乎就是它的设计目的。

标签: asp.net-mvc-4 signalr


【解决方案1】:

所以,我已经使用 SignalR 并按照freedomn-m 告诉我的方法解决了我的问题

我需要在sol_OnSolutionFound 被触发时使用 i can send data 创建一个集线器。

这是我的中心

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

在 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>
}

最后需要在sol_OnSolutionFound 被触发时调用SolutionFound

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

【讨论】:

    猜你喜欢
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 2015-04-30
    • 2015-11-19
    • 2015-11-30
    • 1970-01-01
    相关资源
    最近更新 更多