Reputation: 138
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.
Upvotes: 2
Views: 13806
Reputation: 177
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
});
}
}
Upvotes: 4