Getenumerator error while rendering a View

Asked

Viewed 828 times

3

I did it just as a test:

@model Operador.MVC.Models.OperadorModel

@{
    ViewBag.Title = "Operador";
}

<table id="tblOperador" class="table table-hover table-striped" border="1" cellspacing="0" style="width: 100%;">
<thead>
    <tr>
        <th>Code</th>
        <th>NameHealth</th>   
        <th>NameDental</th>
        <th>LastUpdateDate</th>  
        <th>LastUpdateLogin</th>     
        <th>Brand Code</th>    
        <th>Brand</th>         
    </tr>
</thead>
<tbody>
@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.NameHealth)
        </td>                                             
        <td>
            @Html.TextBoxFor(modelItem => item.NameDental, new { style = "width: 50px;"})
        </td>
    </tr>
}
</tbody>

When I rethink it gives me that mistake:

Server Error in Application '/'.

Build Error

Description: Error when compiling a resource needed to meet this request. Examine the specific details of the error and modify the source code appropriately.

Compiler Error Message: CS1579: Foreach Instruction Cannot operate on variables of type 'Operator.MVC.Models.Operadormodel' because 'Operator.MVC.Models.Operadormodel' does not contain a public definition to 'Getenumerator'

Error of Origin:

Line 20: Line 21: Line 22: @foreach (var item in Model) Line 23: { Line 24:

Archive of Origin: c: Projects_mvc Operator.MVC Views Operator Operator.cshtml Line: 22

Show Compiler Detailed Output:

Show Full Build Source:

Version Information: Microsoft . NET Framework Version:4.0.30319; Version of ASP.NET:4.0.30319.34249

How do I solve the Getenumerator problem? What should I add?

1 answer

5


As the error itself says:

Foreach instruction cannot operate on variables of type 'Operator.MVC.Models.Operadormodel'

Your mistake is here:

foreach (var item in Model)

Model is a variable of the type Operador.MVC.Models.OperadorModel and you can only do foreach in variables that implement GetEnumerator(), such as IEnumerable, List and the like.

  • I get it, just to confirm. If I have a class, say Operadormodelteste and in it I have a method that returns me a list of operators and if I put like this: @model Operator.MVC.Models.Operadormodelteste, so I can make a foreach and fill the TD’s of my table?

  • No. You have to use the method that returns a list.

  • But how do I do it? I don’t understand, I’m sorry for my ignorance, but I don’t understand how I do it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.