2

I'm using mvc and I'm passing the class called DataModel as my model to the view:

public class DataModel
{        
    public List<ServerStats> statsPerMonthPerServer { get; set; }
}

And here is the class ServerStats:

public class ServerStats
{               
    public string ServerID { get; set; }
    public List<ColumnData> ServerMonthStats { get; set; }
}

And here is the class ColumnData:

public class ColumnData
{
    public string Date { get; set; }
    public double Sum { get; set; }
}

I have managed to build my ModelData object, if I debug it, it looks like the information I want is in it, but I can not manage to print it out from the view. This is what I've tried to do:

View:

@model IEnumerable<WebApp.Models.DataModel>
@foreach (var server in Model)
{
foreach (var month in server.statsPerMonthPerServer)
{
    @month.ServerID
    foreach (var column in month.ServerMonthStats)
    {
        @column.Date <br />
        @column.Sum <br />
    }
}    
}

And the controller where I build the DataModel and send it to the view:

DataModel dm = new DataModel();
dm.statsPerMonthPerServer = new List<ServerStats>();

foreach (var row in serverStats)
        {
                ServerStats ss = new ServerStats();
                ss.ServerID = row.ServerID;
                ss.ServerMonthStats = new List<ColumnData>();
                ColumnData cd = new ColumnData();
                cd.Date = row.Year.ToString() + "-" + row.Month.ToString();
                cd.Sum = row.ServerSum;
                ss.ServerMonthStats.Add(cd);                    
                dm.statsPerMonthPerServer.Add(ss);

        }


        return View(dm);

The error I get: The model item passed into the dictionary is of type 'WebApp.Models.DataModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[WebApp.Models.DataModel]'.

I'm using lists but it says this is a dictionary, but I assume it is talking about my lists. I've read some about the IEnumerable interface. Do I have to implement it on all the classes except DataModel? I thought the model decleration on the top of the view took care of this? Any help would be preciated!

1 Answer 1

4

The error message is pretty clear. Your view is strongly typed to IEnumerable<DataModel> but from your controller action you passed only a single DataModel instance to the view. I guess you meant this:

@model WebApp.Models.DataModel
@foreach (var month in Model.statsPerMonthPerServer)
{
    <h3>@month.ServerID</h3>
    @foreach (var column in month.ServerMonthStats)
    {
        @column.Date <br />
        @column.Sum <br />
    }
} 
Sign up to request clarification or add additional context in comments.

2 Comments

@Xerion: Atleast accept a answer when your problem is solved.
@Veer you have to wait a couple of minutes before you can check it, calm down

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.