-2

This is different from NullReferanceException question, because we are dealing with a project that has been separated by concerns, making it different in some ways - as previous answers were dealing with one project, we are working with 3 different projects and namespaces.

I keep getting this:

Line 41:         <th></th>
Line 42:     </tr>
Line 43:     @foreach (var item in Model)
Line 44:     {
Line 45:         <tr>

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

This is my code:

DVD.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Web;
using System.Data.Entity;

namespace DVDStore.Data.Models
{

    public class DVD
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public decimal Price { get; set; }
        //public byte[] Picture { get; set; }
        // Foreign Key
        public virtual Ratings RatingsID { get; set; }
        // Foreign Key
        public virtual Genres GenresID { get; set; }
        // Foreign Key
        public virtual SalesInfo SalesInfoID { get; set; }
        //Foreign Key
        public virtual ICollection<Actor> Actors { get; set; }
    }


    public class MovieDBContext : DbContext
    {
        public DbSet<DVD> DVDs { get; set; }
    }
}

Controller

using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using DVDStore.Access.Methods;

namespace DVDStore.WEB.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index()
        {

        FindAllDVDs findDVDs = new FindAllDVDs();

            return View(findDVDs);
        }
    }

Access

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DVDStore.Data.Models;
using System.Data;
using System.Data.Entity;
using System.Net;
using System.Web.Mvc;

namespace DVDStore.Access.Methods
{
    public class FindAllDVDs
    {
        //List<FindAllDVDs> dVDs = new List<FindAllDVDs>();

        private DVDStoreContext db = new DVDStoreContext();

        public dynamic ViewBag { get; }

        public void FindAllDVD(string DVDTitles, string searchString)
        {
            var FindDVDS = new List<string>();

            var DVDQuery = from d in db.DVD
                           orderby d.Title
                           select d.Title;

            FindDVDS.AddRange(DVDQuery.Distinct());
            ViewBag.DVDTitles = new SelectList(FindDVDS);

            var dvds = from dvd in db.DVD
                       select dvd;

            if (!String.IsNullOrEmpty(searchString))
            {
                dvds = dvds.Where(s => s.Title.Contains(searchString));
            }
            if (!String.IsNullOrEmpty(DVDTitles))
            {
                dvds = dvds.Where(x => x.Title == DVDTitles);

            }

        }

    }

    }

and finally the index.schtml where I'm getting the problem

Everything compiles w/ 0 errors btw

@*@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}*@

@model IEnumerable<DVDStore.Data.Models.DVD>


@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p></p>

@Html.ActionLink("Create New", "Create")


@using (Html.BeginForm("Index", "DVD", FormMethod.Get))
{
    <p>
        Title: @Html.TextBox("SearchString") <br />
        <input type="submit" value="Filter" />
    </p>
}
}
 </p>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ReleaseDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>
        <th></th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ReleaseDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>

        </tr>
    }

</table>

 </table>
alex1983
  • 29
  • 1
  • 10
  • 3
    Of course its a duplicate - it makes no difference if multiple projects of namespaces. The `Model` in `@foreach (var item in Model)` is `null`. And the code in your `public ActionResult Index()` method make no sense. All you have done is initialize an instance of your class. You have not even called your `FindAllDVD()` method, which does not return anything any way. –  Mar 23 '18 at 03:04
  • Alex, there is one single cause of this exception: a value that you are trying to use is null. Therefore every NullReferenceException question is a duplicate. The question yours duplicates explains all the ways things can be null and how to fix it. – ProgrammingLlama Mar 23 '18 at 14:49

3 Answers3

1

Your view is expecting a collection of DVD type model.

@model IEnumerable<DVDStore.Data.Models.DVD>

However, in your Controller, you are passing an instance of FindAllDVDs.

        FindAllDVDs findDVDs = new FindAllDVDs();
        return View(findDVDs);

You should pass a List of DVD objects.

Update:

The quickest way would be to return the list of DVD objects from your method.

public IEnumerable<DVDStore.Data.Models.DVD> FindAllDVD(string DVDTitles, string searchString)

and return the list of Dvds.

Then in your controller, you can do this:

FindAllDVDs findDVDs = new FindAllDVDs();
IEnumerable<Dvd> dvds = findDVDs.FindAllDVD(<DVDTitles>, <searchString>);
return View(dvds);

However, I would not recommend that approach. You don't want to use your data model as your view model. I would rather create a separate DVD class as model for the View, and then map the Data model to this View Model.

ragyaiddo
  • 152
  • 1
  • 4
  • how would i pass that? – alex1983 Mar 23 '18 at 02:37
  • The quickest way would be to return the list of DVD objects from your method.
    `public IEnumerable FindAllDVD(string DVDTitles, string searchString)`
    and return the list Dvds. Then in your controller, you can do this:
    `FindAllDVDs findDVDs = new FindAllDVDs();`
    `IEnumerable dvds = findDVDs.FindAllDVD(, );`
    `return View(dvds);`
    However, I would not recommend that approach. I would rather create a separate DVD class as model for the View, and then map the Data model to this View Model.
    – ragyaiddo Mar 23 '18 at 02:58
  • sorry, my comment is a bit messy, still struggling with this comment formatting. I updated my answer with what I described. – ragyaiddo Mar 23 '18 at 03:09
1

You need to change your FindAllDVD method so that it has:

return dvds;

as its last line. This will likely mean changing the signature to:

public IEnumerable<DVD> FindAllDVD(string DVDTitles, string searchString)

Then, rather than passing findDVDs to the view, you need to pass the result of findDVDs.FindAllDVD instead:

FindAllDVDs findDVDs = new FindAllDVDs();
return View(findDVDs.FindAllDVD("", ""));

This way your Model will be using the set of DVDs rather than the single instance of the FindAllDVDs class like it is now.

mjwills
  • 23,389
  • 6
  • 40
  • 63
1

Inside of FindAllDVDs you have the List<FindAllDVDs> dVDs = new List<FindAllDVDs>(); that is set inside the constructor.

You can just call the constructor as you just did (remember to uncomment the dvds property, like this:

public List<FindAllDVDs> dVDs = new List<FindAllDVDs>();

and set the values inside the constructor, like this:

dVDs = dvds;

Maybe you'll need to do some cast, like this:

dVDs = dvds.toList();

, and pass the value that is inside the FindAllDVDs, like this:

FindAllDVDs findDVDs = new FindAllDVDs("","");

return View(findDVDs.dvds);
Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
  • it doesn't seem to be recognizing the dvds - you mean uncomment the dVDs correct? – alex1983 Mar 23 '18 at 02:37
  • I made a more detailed description now, I meam uncomment the property at the top of `FindAllDVDs` and use that to get the value for the controller. – Felipe Augusto Mar 23 '18 at 02:47