使用mvc从数据库中获取数据

时间:2014-03-07 09:05:46

标签: c# asp.net-mvc entity-framework model-view-controller

我正在尝试从数据库中检索数据并填充表中的记录,但是我收到错误An exception of type 'System.NullReferenceException' occurred in App_Web_14paz0pq.dll but was not handled in user code Additional information: Object reference not set to an instance of an object.我在运行项目时遇到此错误,我的观点甚至没有显示在网络中 - 浏览器。这是我的模特:

public class MovieModel
{
    public int ID { set; get; }
    [DisplayName("First Name")]
    public string FirstName { set; get; }
    [DisplayName("Second Name")]
    public string SecondName { set; get; }
    [DisplayName("Date Of Birth")]
    public DateTime DOB { set; get; }
    [DisplayName("Other Info")]
    public string Other{ set; get; }
}

这是我的控制者:

namespace Movie.Controllers
{
    public class MoviesController : Controller
    {

        private ApplicationDbContext db = new ApplicationDbContext();

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

        [HttpPost]
        public ActionResult GetList(MovieModel model)
        {
            var data = db.Movies.ToList();
            return View(data);
        }
    }
}

这是我的观点:

@model IEnumerable<Movie.Models.MovieModel>

@{
   ViewBag.Title = "";
 Layout = "~/Views/Shared/_Layout.cshtml";
    }  


<p>
    @Html.ActionLink("Create New", "Create")
 </p>
   <table class="table">
    <tr>
    <th>
        @Html.DisplayNameFor(model => model.FirstName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.SecondName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.DOB)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Other)
    </th>
    <th></th>
</tr>



@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.FirstName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.SecondName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.DOB)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Other)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
        @Html.ActionLink("Details", "Details", new { id=item.ID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.ID })
    </td>
</tr>
    }

</table>

我在此行@foreach (var item in Model) {

中收到此错误

3 个答案:

答案 0 :(得分:1)

您在这里获得System.NullReferenceException

@foreach (var item in Model)因为您没有将强类型模型传递给您的视图。所以for循环中的Model是NULL

在你的控制器中试试这个:

public class MoviesController : Controller
{

    private ApplicationDbContext db = new ApplicationDbContext();

    public ActionResult GetList() 
    {
        var model = db.Movies.ToList();
        return View(model);
    }

    [HttpPost]
    public ActionResult GetList(MovieModel model)
    {
        var data = db.Movies.ToList();
        return View(data);
    }
}

答案 1 :(得分:0)

假设它是Razor View并且Movies / GetList是您的默认Url,您可能会尝试直接在视图中使用Model对象而不进行Null检查。请检查您的cshtml C#代码。

如果空引用位于.cs文件中,则不会看到App_Web _ * .dll,因为这是已编译页面的DLL。

答案 2 :(得分:0)

试试这个,

public class MoviesController : Controller
{

  private ApplicationDbContext db = new ApplicationDbContext();

  public ActionResult GetList() 
  {
    var Movies = (from movie in db.Movies select movie).ToList();
    return View(Movies);
  }

  [HttpPost]
  public ActionResult GetList(MovieModel model)
  {
   var data = db.Movies.ToList();
    return View(data);
  }

查看

@model IEnumerable<Movie.Models.MovieModel>

@{
   ViewBag.Title = "";
 Layout = "~/Views/Shared/_Layout.cshtml";
    }  


<p>
    @Html.ActionLink("Create New", "Create")
 </p>
   <table class="table">
    <tr>
    <th>
        @Html.DisplayNameFor(model => model.FirstName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.SecondName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.DOB)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Other)
    </th>
    <th></th>
</tr>

@if (Model != null)
{

foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.FirstName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.SecondName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.DOB)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Other)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
        @Html.ActionLink("Details", "Details", new { id=item.ID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.ID })
    </td>
</tr>
    }
}

</table>
    }