1

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

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

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

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

感谢任何菜鸟的帮助,非常感谢。

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

“视图”的代码在这里:

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


}
}
4

1 回答 1

1

db.Data可能为空。检查它是否正在返回数据。但是,您的 foreach 可以处理这个问题。还要考虑offer1, offer2,offer3可能为空。对空对象调用 Replace 也会引发该错误。

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

于 2012-04-09T21:08:11.110 回答