I have a populated database, But when I want to return a single value in a view Visual studio pointer to a System.NullReferenceException' occurred in App_Web_qoycb13m.dll but was not handled in user code. Why is this happening ?
The line of error is:
<h2>@Model.Name</h2>
The controller:
public ActionResult Detail(int? MovieID)
{
var movie = _movie.Movies.Include(m => m.Genre).SingleOrDefault(m => m.Id == MovieID);
return View(movie);
}
The view:
@model Viddly.Models.Movie
@{
ViewBag.Title = "Detail";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@Model.Name</h2>
The Index page to click and change the page by 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>
And its my 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