简体   繁体   中英

Angular asp.net mvc calling c# controller function

I tried a bunch of stuff and this is what i have so far:

So i try to call this method "runTest()" from my javascript. The method does not actually go, and it goes through as a success, so the "handleTest()" goes and also succeeds. I need it to actually call the controller function.

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# controller method (named TestsController)

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

Any help would be so so appreciated.

An example could be as follows:

In your c# controller

[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);
     }   
}

Try to run your web request using the angular $http service.

 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 
           });
         }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM