Heey,查看错误@Foreach(VAR项目...)型号/控制器/视图
我有这似乎是一个愚蠢的问题,但我不能修复它,我不知道为什么我想我可以俯瞰所以我需要一组新的眼睛。
我有一个模型类:
namespace LaatsteStudentWebAPI.Models
{
public class Student
{
public int StudenNr { get; set; }
public string Naam { get; set; }
public string Woonplaats { get; set; }
public string Adres { get; set; }
public int TelefoonNr { get; set; }
public string School { get; set; }
}
}
和控制器:
namespace LaatsteStudentWebAPI.Controllers
{
public class ArticleController : Controller
{
private List<Student> students = new List<Student>();
private int _nextId = 1;
SqlConnection con;
SqlDataAdapter da;
DataSet ds = new DataSet();
public ActionResult Index()
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
da = new SqlDataAdapter("SELECT StudentNr, Naam, Woonplaats, Adres, TelefoonNr, School FROM [Table] ORDER BY StudentNr DESC", con);
da.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
students.Add(new Student() { StudenNr = int.Parse(dr[0].ToString()), Naam = dr[1].ToString(), Woonplaats = dr[2].ToString(), Adres = dr[3].ToString(), TelefoonNr = int.Parse(dr[4].ToString()), School = dr[5].ToString() });
}
return View(students);
}
}
}
和一个视图:
@model IEnumerable<LaatsteStudentWebAPI.Models.Student>
@{
ViewBag.Title = "Index";
}
<h2>This is the Index</h2>
<table>
<thead>
<tr>
<td>StudentNummer</td>
<td>Naam</td>
<td>Woonplaats</td>
<td>Adres</td>
<td>Telefoonnummer</td>
<td>School</td>
</tr>
</thead>
@foreach (var item in LaatsteStudentWebAPI.Models.Student)
{
<tr>
<td>@item.StudenNr</td>
<td>@item.Naam</td>
<td>@item.Woonplaats</td>
<td>@item.Adres</td>
<td>@item.TelefoonNr</td>
<td>@item.School</td>
</tr>
}
</table>
现在我的问题是,当我运行它看起来一切好吧,(我知道它的凌乱的代码,但只是希望它的工作) 但我得到这个问题/错误。
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0119: 'LaatsteStudentWebAPI.Models.Student' is a 'type', which is not valid in the given context
Line 20:
Line 21:
Line 22: @foreach (var item in LaatsteStudentWebAPI.Models.Student)
Line 23: {
Line 24: <tr>
正在发生的事情错了,它必须是一个小问题,我认为并希望容易解决,我希望你们不能帮我。
收到此错误: 型“System.NullReferenceException”的异常出现在App_Web_jkxy3fxw.dll但在用户代码中没有处理 其他信息:对象引用未设置为一个实例的一个对象。 –
你从哪里得到这个错误? – afzalulh
在同一行,在单词模型。所以我将代码更改为: @foreach(Model中的LaatsteStudentWebAPI.Models.Student项) 但仍然是相同的错误 –