I have looked at the "Subsonic 5 Minute Demo", and got my data objects generated from the database after some initial problems. I am using Visual Web Developer Express 2010 and my project is, as in the demo, an ASP.NET MVC 2 Web Application.
So, I've written this code in my HomeController:
public ActionResult Index()
{
ViewData["Message"] = "Welcome to Food Recipe Web!";
//lets get some data
var db = new FoodRecipeWeb.Data.FoodRecipeWebDB();
var recipes = from r in db.Recipes
select r;
return View(recipes);
}
My View looks like this:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: ViewData["Message"] %></h2>
<ul>
<%foreach (var recipe in Model)
{ %>
<li><%= recipe.Name %></li>
<% } %>
</ul>
</asp:Content>
This is quite like the demo, but when I try to look at the page, I get this message:
Compiler Error Message: CS1579: foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'
Source Error:
Line 6: <h2><%: ViewData["Message"] %></h2>
Line 7: <ul>
Line 8: <%foreach (var recipe in Model)
Line 9: { %>
Line 10:
So what am I doing wrong here?
Your View needs to know about the Model.
Change it from: