简体   繁体   中英

AngularJS + ASP.NET MVC call controller action

Before using AngularJS I used this code to get JSON result of function

$.ajax({
        url: '@Url.Action("getGamedata", "Home")',
        type: 'GET',
        dataType: 'json',
        cache: false,
        async: false,
        success: function (gameInfo) {
            //alert(gameInfo.Name); //Working OK
            for(var i=0;i<6;i++)
                createTable(gameInfo[i]);
        }
    });

JSON result contain 6 items with name, genre, imageUrl and etc. Now I'm using AngularJS and I have function to build dynamic grid

function buildGridModel(tileTmpl) {
          var it, results = [];

          for (var j = 0; j < 6; j++) {

              it = angular.extend({}, tileTmpl);
              it.icon = it.icon + (j + 1);
              it.title = it.title + (j + 1);
              it.span = { row: 1, col: 1 };

              switch (j + 1) {
                  case 1:
                      it.background = "red";
                      break;

                  case 2: it.background = "green"; break;
                  case 3: it.background = "darkBlue"; break;
                  case 4:
                      it.background = "blue";
                      break;

                  case 5:
                      it.background = "yellow";
                      break;

                  case 6: it.background = "pink"; break;
              }

              results.push(it);
          }

          return results;
      }

I want to push each item title to my grid tile title.

  1. 1st tile title = 1st JSON item title
  2. 2nd tile title = 2nd JSON item title
  3. and etc

my solution: put ajax into function

function buildGridModel(tileTmpl) {
          var it, results = [];
          $.ajax({
              url: '/home/getGamedata',
              type: 'GET',
              dataType: 'json',
              cache: false,
              async: false,
              success: function (gameInfo) {

                  for (var j = 0; j < 6; j++) {

                      it = angular.extend({}, tileTmpl);
                      it.icon = it.icon + (j + 1);
                      it.title = gameInfo[j]["Name"];
                      it.span = { row: 1, col: 1 };

                      switch (j + 1) {
                          case 1:
                              it.background = "red";
                              break;

                          case 2: it.background = "green"; break;
                          case 3: it.background = "darkBlue"; break;
                          case 4:
                              it.background = "blue";
                              break;

                          case 5:
                              it.background = "yellow";
                              break;

                          case 6: it.background = "pink"; break;
                          case 7: it.background = "darkBlue"; break;
                          case 8: it.background = "purple"; break;
                          case 9: it.background = "deepBlue"; break;
                          case 10: it.background = "lightPurple"; break;
                          case 11: it.background = "yellow"; break;
                      }

                      results.push(it);
                  }
              }
          });

          return results;
      }

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