Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does not contain a public definition for 'GetEnumerator' in MVC

I am trying to solve this from 2 days.

The error is

Compiler Error Message: CS1579: foreach statement cannot operate on variables of type 'mvc_ado.net_crud.Models.BusinessObject.ContactPerson' because 'mvc_ado.net_crud.Models.BusinessObject.ContactPerson' does not contain a public definition for 'GetEnumerator'

My controller

    public ActionResult List()
    {
        var model = ContactPersonManager.GetList();
        return View(model);
    }

In my model

 public static ContactPersonList GetList()
    {
        ContactPersonList tempList = null;
        using (var myConnection = new SqlConnection(AppConfiguration.ConnectionString))
        {
            var myCommand = new SqlCommand("uspContactPersonSelectList", myConnection) { CommandType = CommandType.StoredProcedure };
            myConnection.Open();
            using (var myReader = myCommand.ExecuteReader())
            {
                if (myReader.HasRows)
                {
                    tempList = new ContactPersonList();
                    while (myReader.Read())
                    {
                        tempList.Add(FillDataRecord(myReader));
                    }
                }
                myReader.Close();
            }
        }
        return tempList;
    }

My view

    @foreach (var person in Model)
    {
        <ul>
            <li>
                @person.ID;
            </li>
        </ul>
    }

Am i missing or doing thing's wrong?

like image 402
Nisar Avatar asked Dec 02 '25 08:12

Nisar


1 Answers

Instead of

@model mvc_ado.net_crud.Models.BusinessObject.ContactPerson

have

@model IEnumerable<mvc_ado.net_crud.Models.BusinessObject.ContactPerson>

in your view.

This assumes that your ContactPersonList implements the IEnumerable<ContactPerson>. If not, then you could try

@model ContactPersonList

however it is not clear what the custom type, ContactPersonList, really is.

like image 135
Wiktor Zychla Avatar answered Dec 04 '25 21:12

Wiktor Zychla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!