从数据库查看数据时出现ASP.Net C#错误

时间:2012-04-09 21:01:31

标签: c# visual-studio-2010 asp.net-mvc-3 foreach

我在使用表单调用我刚刚更新到数据库的一些数据时收到错误。

我正在使用Visual Studio Express 2010 MVC3 Razor(我对所有形式的开发都很陌生)。我填写了表单,单击了更新按钮,该按钮成功地将文本框中的文本存储到表中,但是当我尝试在单独的页面上查看该数据时,我收到此错误:

NullReferenceException was unhandled by user code.
Object reference was not set to an instance of an object.

我确信这对大多数人来说是显而易见的,但我不确定我做错了什么。

感谢任何noob帮助,非常感谢。

=============================================== ===================================

“视图”的代码在这里:

@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>

=============================================== ===================================

,控制器在这里:

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();
    }
}
}

=============================================== ===================================

从数据库读取的控制器:

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);
    }


}
}

1 个答案:

答案 0 :(得分:1)

db.Data可能为null。检查它是否正在返回数据。但是,你的foreach可以解决这个问题。另请注意,offer1, offer2,offer3可能为空。在null对象上调用Replace也会抛出该错误。

另外,请注意人们评论您的代码的建议。有一些非常危险的习惯。