2012-02-24 59 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