我正在设置一个简单的MVC页面来提高自己的技能。它使用ADO.NET运行存储过程,该存储过程可以选择,插入,更新或删除单个数据库表中的行。插入工作正常。其他不是
[HttpGet]
public ActionResult Select()
{
//Models.Employee E = new Models.Employee();
//return View(E);
return View();
}
[HttpPost]//can only be triggered after post from browser
public ActionResult Select(string FirstName, string
LastName,string EmpCode,string Office,string Position,int Salary)
{
List<Models.Employee> Employees = new List<Models.Employee>();
Select C = new Select();
C.RetrieveData(FirstName, LastName,
EmpCode,Office,Position,Salary);
return View("List",Employees);
}
namespace UI.Models
{
public class Employee
{
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmpCode { get; set; }
public string Position { get; set; }
public string Office { get; set; }
public int Salary { get; set; }
}
}
@model IEnumerable<UI.Models.Employee>
@{
ViewBag.Title = "Select";
}
<h2>Select</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.EmpCode)
</th>
<th>
@Html.DisplayNameFor(model => model.Position)
</th>
<th>
@Html.DisplayNameFor(model => model.Office)
</th>
<th>
@Html.DisplayNameFor(model => model.Salary)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmpCode)
</td>
<td>
@Html.DisplayFor(modelItem => item.Position)
</td>
<td>
@Html.DisplayFor(modelItem => item.Office)
</td>
<td>
@Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.EmployeeID }) |
@Html.ActionLink("Details", "Details", new {
id=item.EmployeeID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.EmployeeID })
</td>
</tr>
}
</table>
当我按下我的MVC页面上的选择按钮时,我想查看表中的所有记录。我实际上在这一行上得到了一个空引用异常。
@foreach(模型中的变量项){
你调用的对象是空的。
是控制器,模型还是视图出现问题?
我该如何解决?