Reputation: 1339
I have been following
http://pluralsight.com/training/Courses/TableOfContents/mvc4-building
to learn some MVC C# for my Company, btw completely amazing Video.
I am populating a View with a SQL source.
In Debug I can definitely tell all my connections work, and I get to my foreach loop that should display all the data in that table
On my @Foreach( var item in Model ) it throws the NullRefException on my Model... here's the code I have
this is my complete view
@model IEnumerable<OilNGasWeb.ModelData.Clients>
@{
ViewBag.Title = "CLS-Group";
}
@foreach(var item in Model)
{
<div>
<h4>@item.Client</h4>
<div>@item.Address</div>
<div>@item.City</div>
<div>@item.State</div>
<div>@item.Zip</div>
<div>@item.ContactName</div>
<div>@item.ContactEmail</div>
<div>@item.County</div>
<div>@item.Authorized</div>
<hr />
</div>
}
So I'm thinking it is instantiated here
@model IEnumerable<OilNGasWeb.ModelData.Clients>
but just incase I was wrong maybe it's instantiated in the Home controller in the Index Action?
public ActionResult Index()
{
var Model = _db.Clients.ToList();
return View();
}
Please help me figure out why it's throwing this exception thanks. I wouldn't think you needed more code. but if you do let me know what M , V , C to post for you, as said above the data part works great.
Upvotes: 2
Views: 136
Reputation: 6715
public ActionResult Index()
{
var model = _db.Clients.ToList();
return View(model);
}
You need to pass the model to the view, otherwise it will be null.
Upvotes: 6