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>