我有一个填充的数据库,但是当我想在视图中返回单个值时,Visual Studio指向System.NullReferenceException'发生在App_Web_qoycb13m.dll但未在用户代码中处理。为什么会这样?
错误行是:
<h2>@Model.Name</h2>
控制器:
public ActionResult Detail(int? MovieID)
{
var movie = _movie.Movies.Include(m => m.Genre).SingleOrDefault(m => m.Id == MovieID);
return View(movie);
}
观点:
@model Viddly.Models.Movie
@{
ViewBag.Title = "Detail";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@Model.Name</h2>
索引页面,按MovieID单击并更改页面:
@model IEnumerable<Viddly.Models.Movie>
@{
ViewBag.Title = "Movie";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Movies</h2>
<table class="table table-bordered table-hover">
<tbody>
<thead>
<tr>
<th>Movies</th>
<th>Genre</th>
</tr>
</thead>
@foreach (var movie in Model)
{
<tr>
<td>@Html.ActionLink(movie.Name, "Detail", new { movie.Id})</td>
<td>@movie.Genre.GenreType</td>
</tr>
}
</tbody>
</table>
它的我的RouteConfig.cs:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "CustomerDetails",
url: "{controller}/{Detail}/{CustomerID}",
defaults: new { controller = "Customer", action = "Detail" }
);
routes.MapRoute(
name: "MovieDetails",
url: "{controller}/{Detail}/{MovieID}",
defaults: new { controller = "Movie", action = "Detail" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
EDITED