2012-03-12 3 views
0

내 mvc 액션에서 내 뷰로 두 개의 dimentional 배열을 반환하고 객체를 $ ite() 함수를 사용하여 반복하고 싶습니다. 웬일인지 나는 이것을 작동시킬 수 없다.2 차원 배열을 json 객체로 반환하고 반복합니다.

내 조치 :

[HttpPost] 
    public JsonResult ShowTiles(TileModel tile) 
    { 

     MapModel map = HomeController.mapModel; 
     map.MapTilesArray[tile.OldX, tile.OldY].Value = 4; 

     var player = map.MapTilesArray[tile.OldX, tile.OldY].Player; 
     map.MapTilesArray[tile.X, tile.Y].Player = player; 
     player.Position = new Point(tile.X,tile.Y); 

     map.MapTilesArray[tile.X, tile.Y].Value = 2; 

     return Json(map.MapTilesArray); 
    } 

내 모델 :

public MapModel() 
    { 

     MapTilesArray = new TileModel[10,10]; 

     for (int i = 0; i < 10; i++) 
     { 
      for (int j = 0; j < 10; j++) 
      { 
       MapTilesArray[i, j] = new TileModel(); 
       MapTilesArray[i, j].Value = 1; 

      } 
     } 

     userNameList = new List<string>(); 
    } 

내보기 :

$.ajax({ 
      beforeSend: function() { ShowAjaxLoader(); }, 
      url: "/Game/ShowTiles", 
      type: "POST", 
      contentType: "application/json;charset=utf-8", 
      dataType: "json", 
      data: JSON.stringify(tile), 
      success: function (data) { 
       HideAjaxLoader(), 
       $.each(data, function (index, item) { 
        if (item.Value === 0) { 
         $("#" + item.Y + item.X).css("background-color", "brown"); 
        } 
        else if (item.Value === 1) { 
         $("#" + item.Y + item.X).css("background-color", "black"); 
        } 
        else if (item.Value === 2) { 
         $("#" + item.Y + item.X).css("background-color", "green"); 
        } 
        else if (item.Value === 3) { 
         $("#" + item.Y + item.X).css("background-color", "blue"); 

        } 
        else if (item.Value === 4) { 
         $("#" + item.Y + item.X).css("background-color", "purple"); 

        } 

       }), MovePlayer(newPosition.left + 15, newPosition.top + 15) 
      }, 
      error: function() { showErrorMsg(); } 
     }); 
    }); 

답변

0

당신이

같은 시도 컨트롤러 에코 배열

$data = array(
    array(
     'title' => 'My title' , 
     'name' => 'My Name' , 
     'date' => 'My date' 
    ), 
    array(
     'title' => 'Another title' , 
     'name' => 'Another Name' , 
     'date' => 'Another date' 
    ) 
); 

echo json_encode($data); 

당신은보기에 그것을 얻을 이해하고 senario에 적용이

var data = $.parseJSON(data); 


for(x in data) 
{ 
    alert(data[x]['title]); 
    alert(data[x]['name]); 
    alert(data[x]['date]); 
} 

시도처럼 반복 할 수