Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
up vote 2 down vote accepted

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 
           });
         }
    }
share|improve this answer
    
What you added did not fully fix my issue, but it put me on the right track, and i got it to work, thanks!!! – Richard Sussan Aug 11 '16 at 13:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.