Hi AliYilmaz,
To fix this error creating a class and return strongly type model instead of anonymous object.
Refer below example.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Model
public class Customer
{
public string CustomerID { get; set; }
public string ContactName { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
Controller
public class HomeController : Controller
{
private DBCtx Context { get; }
public HomeController(DBCtx _context)
{
this.Context = _context;
}
public IActionResult Index()
{
var res = (from customer in this.Context.Customers.Take(5)
select new CustomerModel { Name = customer.ContactName }).ToList();
ViewBag.Artikel = res;
return View();
}
public class CustomerModel
{
public string Name { get; set; }
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@foreach (var item in ViewBag.Artikel)
{
@item.Name
<br />
}
</body>
</html>
Output
Maria Anders
Ana Trujillo
Antonio Moreno
Thomas Hardy
Christina Berglund