#我收到此错误,我不知道为什么我想请您帮助我解决它
我不知道怎么了,为什么会给出错误 Object reference not set to an instance of an objector of
“ /”应用程序中的服务器错误。 你调用的对象是空的。 说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取更多信息 有关错误及其在代码中起源的信息。
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 19: </tr>
Line 20:
Line 21: @foreach (var item in Model) {
Line 22: <tr>
Line 23: <td>
我的控制器类如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vidly.Models;
namespace vidly.Controllers
{
public class CustomerController : Controller
{
// GET: Customer
public ActionResult Index()
{
var customerlist = new List<Customer>();
customerlist.Add(new Customer { Name = "Shayan", id = 1 });
customerlist.Add(new Customer { Name = "usman", id = 2 });
customerlist.Add(new Customer { Name = "Affan", id = 3 });
return View();
}
}
}
我的模型课是在
下 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace vidly.Models
{
public class Customer
{
public int id { get; set; }
public string Name { get; set; }
}
}
我的看法与
相同 @model IEnumerable<vidly.Models.Customer>
{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Details", "Details", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}
</table>
必须更改哪个代码段以及为什么