I'm getting an error when recalling some data I've just updated to the database from using a form.
I'm using Visual Studio Express 2010 MVC3 Razor (and I'm very new to all forms of development). I've filled out the form, clicked the update button which is successfully storing the text in the textboxes to the table, but then when I try to view that data on a seperate page I get this error:
NullReferenceException was unhandled by user code.
Object reference was not set to an instance of an object.
I'm sure this is obvious to most, but I'm unsure what I've done wrong.
Appreciate any noob help please, many thanks.
==================================================================================
the code for the "View" is here:
@model IEnumerable<bhgc.Models.Data>
@{
ViewBag.Title = "Special Offers";
}
<fieldset>
<legend><h1>Special Offers</h1></legend>
@foreach (var data in Model)
{
<table>
<tr>
<td>
<strong>
@Html.Raw(data.offer1.Replace(Environment.NewLine, "<br/>"))
</strong>
</td>
<td>
<strong>
@Html.Raw(data.offer2.Replace(Environment.NewLine, "<br/>"))
</strong>
</td>
</tr>
<tr>
<td>
<strong>
@Html.Raw(data.offer3.Replace(Environment.NewLine, "<br/>"))
</strong>
</td>
</tr>
</table>
}
</fieldset>
==================================================================================
and the controller is here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using bhgc.Models;
namespace bhgc.Controllers
{
public class HomeController : Controller
{
private DataDBContext db = new DataDBContext();
public ActionResult Index()
{
return View(db.Data.ToList());
}
public ActionResult Special()
{
return View();
}
public ActionResult Contact()
{
return View();
}
}
}
==================================================================================
Controller that reads from database:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using bhgc.Models;
namespace bhgc.Controllers
{
[Authorize] //Created Validataion so inaccessible from outside
public class DataController : Controller
{
private DataDBContext db = new DataDBContext();
//
// GET: /Data/
public ViewResult Index()
{
return View(db.Data.ToList());
}
//
// GET: /Data/Details/5
public ViewResult Details(string id)
{
Data data = db.Data.Find(id);
return View(data);
}
//
// GET: /Data/Update
public ActionResult Update()
{
var model = db.Data.FirstOrDefault();
return View(model);
}
//
// POST: /Data/Update
[HttpPost]
[ValidateInput(false)]
public ActionResult Update(Data data)
{
if (ModelState.IsValid)
{
data.ID = 1; //EF need to know which row to update in the database.
db.Entry(data).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Special", "Home");
}
return View(data);
}
}
}
db.Datais null - have you used the debugger to find out exactly which variable is null and where?. I have no idea why, because you haven't shown the code that reads from the database.DBContextexplicitly. Either through ausingblock or withmyContextName.Dispose(). Moreover, sending that entire context to the view is definitely not advised. Look into "view models" in mvc3.