【问题标题】:Angular asp.net mvc calling c# controller functionAngular asp.net mvc调用c#控制器函数
【发布时间】:2016-12-17 07:48:09
【问题描述】:

我尝试了很多东西,这就是我目前所拥有的:

所以我尝试从我的 javascript 中调用此方法“runTest()”。 该方法实际上并没有通过,它作为成功通过,所以“handleTest()”通过并且也成功了。我需要它来实际调用控制器函数。

Javascript/Angular/JQuery

$scope.runTest = function (myTest) {
    $.ajax({
        method: 'POST',
        Url: 'Tests/runTest',
        contentType: 'application/json;',
        data: myTest,
        success: function () { handleTest(myTest); },
        error: function () { alert('no object found'); }
    });
}

function handleTest(currentTest){
    var updated = { id: currentTest.id, name: currentTest.name, schedule: currentTest.schedule, description: currentTest.description, server: currentTest.server, port: currentTest.port, method: currentTest.method };
    //var updated = currentTest;
    $http({
        method: "PUT",
        url: 'http://~~api address~~/api/tests/' + (currentTest.id),
        data: updated
    })
    .success(function (data, status, headers) {
        alert("Test was successfully updated.");
        $state.reload();
    })
    .error(function (data, status, headers) {
        alert("Test could not be updated.");
        $state.reload();
    });
}

C# 控制器方法 (命名为 TestsController)

[HttpPost]
public ActionResult runTest(StandardTest myTest)
{
    myTest.lastResult = MyEnum.Pass;
    log.Info(myTest.name + " " + myTest.lastResult + " " + myTest.id);
    return Json(myTest, JsonRequestBehavior.AllowGet);
}

任何帮助将不胜感激。

【问题讨论】:

    标签: c# angularjs asp.net-mvc asp.net-web-api2


    【解决方案1】:

    一个例子如下:

    在你的 c# 控制器中

    [HttpPost]
    public ActionResult runTest(StandardTest myTest)
    {
        myTest.lastResult = MyEnum.Pass;
        log.Info(myTest.name + " " + myTest.lastResult + " " + myTest.id);
    
      if (!testPassed){        
           //test did not pass
       return Json(new {success = false,
                        responseText = "Test did not pass",JsonRequestBehavior.AllowGet);
       }
       else
       {
         //Test Passed
         return Json(new {success = true, 
                          responseText= "Test Passed"},JsonRequestBehavior.AllowGet);
         }   
    }
    

    尝试使用 angular $http 服务运行您的网络请求。

     angular.module('YourModuleName')
            .controller("ngControllerName", ["$http", "$scope", function ($http, $scope) {
            /*Request to C# Controller*/ 
            $scope.runTest = function(myTest){
                var config = {
                    params:{myTest: myTest}
                    }
            $http.get('/Tests/runTest', config).success(function (data) {
              if(data !=null && data.success){
                handleTest(myTest);
               }
            }).error(function (error) {
                 //Handle Error 
               });
             }
        }
    

    【讨论】:

    • 你添加的内容并没有完全解决我的问题,但它让我走上了正确的轨道,我得到了它的工作,谢谢!!!
    • 不应该是 $http.post 而不是 get 吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 2014-10-16
    • 2011-05-27
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    相关资源
    最近更新 更多