0

I created my model to be a multiple array myArr[10,10]. Then I want that model to be returned to my view and I want to be able to check what values are in the array.

My code: model

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

view

  @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>
 }

when I run this i have error The model item passed into the dictionary is of type 'System.Int32[,]', but this dictionary requires a model item of type 'GraAjaxTeamProject.Models.MapModel'.

1 Answer 1

2

You are passing the MapModel to your view. Either change @model to int[,] or remove .MapTilesArray from your call to view.

From the example given in your view, you should be changing the lines

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

    return View(map);
}

To

public ActionResult Index()
{
    return View(mapModel);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.