1

我正在做一个 ASP.NET MVC 教程。我创建了一个晚餐控制器以及一个晚餐控制器的索引视图。但是,我的晚餐索引视图出现错误。错误出现在以下代码中的 for each 循环中。但是晚餐索引视图是默认生成的。我没有改变任何东西。

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

@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 数据模型。

4

2 回答 2

1
return View();

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

于 2013-06-03T13:53:31.697 回答
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);
    }
}
于 2013-06-03T13:55:44.347 回答