1

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'

5
  • What has gone wrong? I don't understand anything from your question. What issue iterating? What are you trying to do? That doesn't look like a view model at all if you are going to dump your database in it. Commented Nov 20, 2010 at 20:54
  • What string exactly. Stop using vars in code examples. We're no compilers. Commented Nov 20, 2010 at 21:21
  • @the_drow, what exactly is your problem with var? Can't you look 5 pixels right from the assignment operator to know the type? I think var is one of the greatest additions to the C# language as it makes it much more concise. So in the OPs case we have a ToList() extension method which more than obviously returns a List<Category> and as far as the ClassifiedsIndexViewModel type is concerned, I think it doesn't deserve any comment. You might say that in the case of List<Category> you don't know how the Category class looks like but even without var you wouldn't know it... Commented Nov 20, 2010 at 22:34
  • ... As a side note if he hadn't used var there would have been a horizontal scrollbar in the code snippets which would have made things even worse :-) Commented Nov 20, 2010 at 22:40
  • This is a generally bad idea to use var in code examples. I can understand why it's useful but it makes the code less clear about your intentions. In real code it's appropriate whenever you have very long type names (in c++ to get an iterator you'll have to type std::list<T>::iterator instead of typing auto). Commented Nov 20, 2010 at 22:46

1 Answer 1

3

Ok so instead of:

foreach (string categoryName in Model.Categories)

do:

<% foreach (var category in Model.Categories) { %>
    <div><%: category.Name %></div>
<% } %>

or:

<% foreach (Category category in Model.Categories) { %>
    <div><%: category.Name %></div>
<% } %>

or even better: use display templates and never write a single foreach in your view:

<%: Html.DisplayFor(x => x.Categories) %>

and in ~/Views/YourControllerName/DisplayTemplates/Category.ascx:

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AfvClassifieds.Models.Category>" %>
<div><%: Model.Name %></div>
Sign up to request clarification or add additional context in comments.

2 Comments

@JHarley1, next time include the error message you are getting. It makes things clear at first sight. Also don't forget the green tick if this answer was helpful to you.
Cheers Darin. The answer was v.helpful. I had to wait for a period of time before I could mark the answer as correct.

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.