This is the ViewModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AfvClassifieds.Models;
namespace AfvClassifieds.ViewModels
{
public class ClassifiedsIndexViewModel
{
public List<Category> Categories { get; set; }
}
}
Let me explain this one, I want to capture everything from my Category table. I then want to pass it to my view using a "strongly typed view". This I populate my new ViewModel:
// Retrieve the categories table from the database.
var categoryModel = AfvClassifiedsDB.Categories.ToList();
// Set up our ViewModel
var viewModel = new ClassifiedsIndexViewModel()
{
Categories = categoryModel,
};
Then I want to iterate through my table in the view: (This is were its gone wrong).
<%
foreach (string categoryName in Model.Categories)
{
%>
I think you could summarise my problem as an issue of iterating through a list in C#?
The error is as follows:
Cannot convert type 'AfvClassifieds.Models.Category' to 'string'
var? Can't you look 5 pixels right from the assignment operator to know the type? I thinkvaris one of the greatest additions to the C# language as it makes it much more concise. So in the OPs case we have aToList()extension method which more than obviously returns aList<Category>and as far as theClassifiedsIndexViewModeltype is concerned, I think it doesn't deserve any comment. You might say that in the case ofList<Category>you don't know how theCategoryclass looks like but even withoutvaryou wouldn't know it...varthere would have been a horizontal scrollbar in the code snippets which would have made things even worse :-)