0

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

martur94
  • 111
  • 9
  • Which values is `movie`? – kat1330 Feb 10 '17 at 21:19
  • is a model Movie type. – martur94 Feb 10 '17 at 21:20
  • 1
    `SingleOrDefault(m => m.Id == MovieID)` Well, obviously you're hitting a movie that doesn't exist in Movies, returning `null`. That's what `SingleOrDefault` *does*. Since that can absolutely happen, do null checking in your razor page. The linked dupe contains that information. –  Feb 10 '17 at 21:20
  • 1
    Because `.SingleOrDefault()` returned `null` therefore your pass `null` to the view and you cannot access a property of `null` –  Feb 10 '17 at 21:21
  • more i check on database, And the parameter sent its not a null value – martur94 Feb 10 '17 at 21:21
  • Put a breakpoint on @Model.Name and examine the model at runtime. –  Feb 10 '17 at 21:25
  • Your issue is right here: `var movie = _movie.Movies.Include(m => m.Genre).SingleOrDefault(m => m.Id == MovieID); if (movie == null) { // what do you want to do? } return View(movie);` – CodingYoshi Feb 10 '17 at 21:27
  • humm all right if i made movieId = 1 and not receive on parameter its work. Why its not passing with parameter ? – martur94 Feb 10 '17 at 21:30
  • You have not shown how you generate the link to that method so how can we know. Enter `../Detail?MovieID=1` in your address bar and it will work fine. –  Feb 10 '17 at 22:32
  • @StephenMuecke its edited bro. – martur94 Feb 11 '17 at 05:20
  • `new { MovieID = movie.Id }` :) - look at the html you were previously generating - the `href` attribute of the links –  Feb 11 '17 at 12:56
  • @StephenMuecke its workkkkkk broo =) Thank you you are the best – martur94 Feb 11 '17 at 18:57

0 Answers0