Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
my foreach in view:
@foreach (var item in Model)
               {
               <tr>
                   <td>
                       @Html.DisplayFor(modelItem => item.ProdutoId)
                   </td>
                   <td>
                       @Html.DisplayFor(modelItem => item.Nome)
                   </td>
                   <td>
                       @Html.DisplayFor(modelItem => item.Categoria.Nome)
                   </td>
                   <td>
                       @Html.DisplayFor(modelItem => item.Fabricante.Nome)
                   </td>
                   <td>
                       @Html.ActionLink("Alterar Produto", "Edit", new { id = item.ProdutoId }) |
                       @Html.ActionLink("Detalhe Fabricante", "Details", new { id = item.ProdutoId }) |
                       @Html.ActionLink("Apagar Fabricante", "Delete", new { id = item.ProdutoId })
                   </td>
               </tr>
               }


What I have tried:

my class 'Produto' in Models:
public class Produto
{
    public long? ProdutoId { get; set; }
    public string Nome { get; set; }

    public long? FabricanteId { get; set; }
    public long? CategoriaId { get; set; }

    public Fabricante Fabricante { get; set; }
    public Categoria Categoria { get; set; }
}
Posted
Updated 7-Jul-20 1:03am
Comments
DerekT-P 6-Jul-20 15:24pm    
You don't give us all the relevant code. But in the first snippet, you're iterating over items within something call "Model". In your second snippet you're referring to a class in "Model𝘀". My guess is that you've populated a collection called "Models" by creating separate instances of Produto called "Model" and adding them to the collection. Fix the typo to iterate over the "Model𝘀" collection instead of the single instance "Model" and you should be OK.

hello thank you very much for your help.
I find the cause of the problem, when binding between the model and the View I didn't define the enumerable interface
 
Share this answer
 
OK. The error means you're trying to iterate over a single instance of your class instead of a collection of objects.

The problem is not in the code you posted, but in the controller code that returns the View. Your controller code is supplying a single instance of "Produto" instead of a collection of "Produto" objects to the View.
 
Share this answer
 
Comments
Raul_Mateia_da_Silva 6-Jul-20 15:35pm    
look my controller:

public class ProdutosController : Controller
{
private EFContext context = new EFContext();

// GET: Produtos
public ActionResult Index()
{
return View(context.Produtos.OrderBy(c=>c.Nome));
}
}
Dave Kreskowiak 6-Jul-20 15:58pm    
Yeah, you're returning a single object that is an IQueryable. This is a query that has been assembled but no executed yet.

Put a call to ToList() on the end of that query. That will return a IList<produto>. Also, unroll your compound statements. Put the query result into a variable then pass that variable to the View object. This makes your code much more debuggable, giving you the ability to see the result in the debugger before it's returned to the View.
public class ProdutosController : Controller
{
    // GET: Produtos
    public ActionResult Index()
    {
        using (var content = new EFContext())
        {
            var queryResult = context.Produtos.OrderBy(c => c.Nome).ToList();
            return View(queryResult);
        }
    }
}

Oh, and your implementation of a class-wide context object is flawed. The context does not get Disposed immediately when no longer in use. You open your connection to the database as late as possible and close/dispose of it when your query is done as soon as possible. This limits resource and license use on the SQL server.
Read the error message, it's pretty clear:
foreach statement cannot operate on variable of type 'produto' because 'produto' das not contain a public instance definition of getenumerator

Include the IEnumerable interface in your class definition, and implement the required method (GetEnumerator) IEnumerable Interface (System.Collections) | Microsoft Docs[^]

Only if it supports IEnumerable can a class be used as a collection for foreach<code> loops
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900