System.NullReferenceException-NerdDinner教程

时间:2013-06-03 13:50:21

标签: asp.net-mvc-3 razor

我正在做一个ASP.NET MVC教程。我创建了晚餐控制器以及晚餐控制器的索引视图。但是,我的晚餐索引视图出错了。以下代码中的每个循环都会显示错误。但是默认情况下会生成晚餐索引视图。我没有改变任何事情。

我的晚餐索引视图的代码是

@model IEnumerable<NerdDinner.Models.Dinner>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            Title
        </th>
        <th>
            Latitude
        </th>
        <th>
            Longitude
        </th>
        <th>
            EventDate
        </th>
        <th>
            ContactPhone
        </th>
        <th>
            Address
        </th>
        <th>
            Country
        </th>
        <th>
            HostedBy
        </th>
        <th>
            Description
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {//Error appears here
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Latitude)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Longitude)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EventDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ContactPhone)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Country)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.HostedBy)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.DinnerID }) |
            @Html.ActionLink("Details", "Details", new { id=item.DinnerID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.DinnerID })
        </td>
    </tr>
}

</table>

我的晚餐控制器的代码是

namespace NerdDinner.Controllers
{
    public class DinnerController : Controller
    {
        IDinnerRepository _repository;

        public DinnerController()
        {
            _repository = new sqlDinnerRepository();
        }

        public DinnerController(IDinnerRepository repository)
        {
            _repository = repository;
        }

        //
        // GET: /Dinner/

        public ActionResult Index()
        {
            if (Request.IsAjaxRequest())
            {
                var dinners = _repository.FindAllDinners().Where(x => x.EventDate >= DateTime.Now).ToList();
                return Json(dinners);
            }
            else
            {
                return View();
            }
        }

        //
        // GET: /Dinner/Details/5

        public ActionResult Details(int id)
        {
            var dinner = _repository.GetDinner(id);
            return View(dinner);
        }

        //
        // GET: /Dinner/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Dinner/Create

        [HttpPost]
        public ActionResult Create(Dinner dinner)
        {
            try
            {
                // TODO: Add insert logic here
                _repository.AddDinner(dinner);
                _repository.Save();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(dinner);
            }
        }

        //
        // GET: /Dinner/Edit/5

        public ActionResult Edit(int id)
        {
            var dinner = _repository.GetDinner(id);
            return View(dinner);
        }

        //
        // POST: /Dinner/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {

            var dinner = _repository.GetDinner(id);
            try
            {
                // TODO: Add update logic here
                UpdateModel(dinner, collection.ToValueProvider());
                _repository.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(dinner);
            }
        }

        //
        // POST: /Dinner/Delete/5

        [HttpPost]
        public ActionResult Delete(int id)
        {
            var dinner = _repository.GetDinner(id);
            try
            {
                // TODO: Add delete logic here
                _repository.DeleteDinner(dinner);
                _repository.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(dinner);
            }
        }
    }
}

IDinnerRepository是一个接口,sqlDinnerRepository实现它

IDinnerRepository的代码是

    namespace NerdDinner.Models
    {
       public interface IDinnerRepository
        {
           //Query Methods
            IQueryable<Dinner> FindAllDinners();
            IQueryable<Dinner> FindUpcomingDinners();
            Dinner GetDinner(int id);

           //Insert/Delete
            void AddDinner(Dinner dinner);
            void DeleteDinner(Dinner dinner);

           //Persistence
            void Save();
        }
    }

sqlDinnerRepository的代码是

namespace NerdDinner.Models
    {
        public class sqlDinnerRepository:   IDinnerRepository
        {
            public dbDataContext db;
            public sqlDinnerRepository()
            {
                db = new dbDataContext();
            }

            public IQueryable<Dinner> FindAllDinners()
            {
                return db.Dinners;
            }

            public IQueryable<Dinner> FindUpcomingDinners()
            {
                return from dinner in db.Dinners
                       where dinner.EventDate > DateTime.Now
                       orderby dinner.EventDate
                       select dinner;
            }

            public Dinner GetDinner(int id)
            {
                return db.Dinners.SingleOrDefault(x => x.DinnerID == id);
            }

            public void AddDinner(Dinner dinner)
            {
                db.Dinners.InsertOnSubmit(dinner);
            }

            public void Save()
            {
                db.SubmitChanges();
            }

            public void DeleteDinner(Dinner dinner)
            {
                db.Dinners.DeleteOnSubmit(dinner);
            }
        }
    }

我已经更新了我的数据库以及LINQ数据模型。

2 个答案:

答案 0 :(得分:1)

return View();

您没有将任何模型传递给视图 因此,Model为空。

答案 1 :(得分:0)

当您发出常规GET请求时,此操作不会将模型发送到视图。

public ActionResult Index()
        {
            if (Request.IsAjaxRequest())
            {
                var dinners = _repository.FindAllDinners().Where(x => x.EventDate >= DateTime.Now).ToList();
                return Json(dinners);
            }
            else
            {
                return View();
            }
        }

我不知道为什么默认项目会这样做但你可以像这样修复NRE:

public ActionResult Index()
{
    var dinners = _repository.FindAllDinners().Where(x => x.EventDate >= DateTime.Now).ToList();
    if (Request.IsAjaxRequest())
    {
        return Json(dinners);
    }
    else
    {
        return View(dinners);
    }
}