2012-02-24 58 views
0

我將我的模型創建爲多個數組myArr [10,10]。 然後,我希望該模型返回到我的視圖,我想能夠檢查數組中的值。傳遞多個數組來查看

我的代碼: 模型

public class MapModel 
{ 
    private int[,] mapTilesArray = new int[10, 10]; 

    public int[,] MapTilesArray 
    { 
     get { return mapTilesArray; } 
     set 
     { 
      mapTilesArray = value; 
     } 

    } 

    public MapModel() 
    { 
     for (int i = 0; i < 10; i++) 
      for (int j = 0; j < 10; j++) 
      { 
       mapTilesArray[i, j] = 1; 
      }; 
    } 

CONTROLER

public MapModel mapModel = new MapModel(); 

    public ActionResult Index() 
    { 
     var map = mapModel.MapTilesArray; 

     return View(map); 
    } 

視圖

@model GraAjaxTeamProject.Models.MapModel 

@{ 
ViewBag.Title = "Home Page"; 
} 


@for (int i = 0; i < 10; i++) 
{ 
<div> 
@for (int j = 0; j < 10; j++) 
{ 
    if (Model.MapTilesArray[i,j] == 1) 
{ 
    <div style="height:30px;width:30px;border:1px solid gray;background-color:red;display:inline-block;"></div> 
} 


} 
</div> 
} 

當我運行此我有錯誤傳遞到字典的模型項的類型爲「 System.Int32 [,]',但是這個字典需要一個'GraAjaxTeamPro'類型的模型項ject.Models.MapModel」。

回答

2

您正在將MapModel傳遞給您的視圖。將@model更改爲int[,]或從您的呼叫中刪除.MapTilesArray以查看。

從您的視圖給出的例子,你應該改變線

public ActionResult Index() 
{ 
    var map = mapModel.MapTilesArray; 

    return View(map); 
} 

public ActionResult Index() 
{ 
    return View(mapModel); 
} 
+0

哇,簡單的答案和它的工作謝謝! – 2012-02-24 12:29:29

+0

我需要等10分鐘 – 2012-02-24 12:41:13