Home > Enterprise >  Why am I Getting this error" foreach statement cannot operate on variables of type ''
Why am I Getting this error" foreach statement cannot operate on variables of type ''

Time:12-20

I am getting this error in my razor view

Severity Code Description Project File Line Suppression State Error CS1579 foreach statement cannot operate on variables of type 'Kitap' because 'Kitap' does not contain a public instance definition for 'GetEnumerator' ilk C:\Users\90537\source\repos\ilk\ilk\Views\Home\Index5.cshtml 17 Active

Here is the controller method:

 public IActionResult Index5()
        {
            var ktp = new List<Kitap>() {
                new Kitap(){Id=1,KitapAd="alem",Yazar="verne"},
                new Kitap(){Id=2,KitapAd="mektup",Yazar="zwayg"},
                new Kitap(){Id=3,KitapAd="noel" ,Yazar="ben"}

            };
            return View(ktp);
        }

And Here is the rest of my code

  @model ilk.Models.Kitap

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index5</title>
</head>
<body>

    <table>
        @foreach (var ks in Model)
        {
            <tr>
                <td>ks.</td>
            </tr>

        }
    </table>

CodePudding user response:

Try to modify your code in the View page as below:

  @model List<ilk.Models.Kitap>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index5</title>
</head>
<body>

    <table>
        @foreach (var ks in Model)
        {
            <tr>
                <td>ks.</td>
            </tr>

        }
    </table>
  • Related