c# - Angular asp.net mvc调用c# Controller 函数

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

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

所以我尝试从我的 JavaScript 中调用这个方法“runTest()”。 该方法实际上并没有执行,并且执行成功,因此“handleTest()”执行并且也成功。我需要它来实际调用 Controller 函数。

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 方法 (名为 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# 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);
     }   
}

尝试使用 Angular $http 服务运行您的 Web 请求。

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

关于c# - Angular asp.net mvc调用c# Controller 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38881663/

相关文章:

c# - 使用自定义 TTF 字体进行 DrawString 图像渲染

angularjs - 我可以在 angularJS 中有条件地禁用过滤器吗?

angularjs - AngularJS 的 cookie 过期了?

AngularJS 将 requestVerificationToken 传递给服务

c# - IEnumerable<> 在未引用的程序集中定义 - 新的 NuGet 类库项目

c# - 获取枚举值

asp.net-mvc - Breeze .js : how to transform a Breeze entity to a plain JSON object

c# - .NET 中数组的泛型函数

c# - 中文本地化支持

c# - 正则表达式替换匹配全部而不是单个