2

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.

1 Answer 1

4

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 
           });
         }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

What you added did not fully fix my issue, but it put me on the right track, and i got it to work, thanks!!!
Isn't it suppose to be $http.post and not get ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.