我正在尝试创建一个简单的MVC项目,并且正在按照本教程从数据库中获取数据。 https://www.aspsnippets.com/Articles/Display-Show-data-records-from-Database-Table-in-ASPNet-MVC.aspx
并查看代码
@model IEnumerable<Bittu2.student1>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table>
<tr>
<th>Student ID</th>
<th>Name</th>
<th>Branch</th>
<th>Mobile</th>
</tr>
@foreach(student1 student1 in Model)
{
<tr>
<td>@student1.StudentId</td>
<td>@student1.Name</td>
<td>@student1.Branch</td>
<td>@student1.Mobile</td>
</tr>
}
</table>
</body>
</html>
控制器代码
namespace Bittu2.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
StudentDemoEntities studentDemoEntities = new StudentDemoEntities();
return View( from student1 in studentDemoEntities.student1.Take(10) select student1);
}
编辑2:
我在student1.cs
类中做了一些更改,即我实现了Enumerable接口并定义了它的GetEnumerator方法
namespace Bittu2
{
using System;
using System.Collections;
using System.Collections.Generic;
public partial class student1 : IEnumerable
{
public int StudentId { get; set; }
public string Name { get; set; }
public string Branch { get; set; }
public Nullable<int> Mobile { get; set; }
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
}
我想了解上面代码中真正发生的事情。只是为了得到一些澄清。