I’m a begginer in asp.net , mvc.I try to populate a table and I receive this error :
foreach statement cannot operate on variables of type
‘K_Api.Notification’ because ‘K_Api.Notification’ does not contain a
public definition for ‘GetEnumerator’
I have a Controller and here I call my view with one parameter.This parameter is a list with data that I want to find in my grid(3 columns: from,subject,status;In my list I have 3 rows).
This is the definition for my list :
List<Notification> sNotificationList=new List<Notification>(); //Notification is a class with the 3 columns and their types
and this is the call to my view :
return View("NotificationsList", sNotificationList);
In notificationList.aspx first row is:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Child.Master" Inherits="System.Web.Mvc.ViewPage<K_Api.Notification>" %>
Then I have :
<%
foreach (K_Api.Notification s in Model)
{
%>
<tr>
<td> <%= Model.From %></td>
<td> <%= Model.Status %></td>
<td> <%= Model.Subject%></td>
</tr>
<%
}%>
Can somebody please tell me where is the error?
Your model definition in the view is wrong. You need your model to be an
IEnumerable<K_Api.Notification>if you want to be able to loop through it. So modify your view in order to be conform to the type you are passing from the controller action:This obviously supposes that your controller action passes an
IEnumerable<Notification>to this view: