【问题标题】:Get data from the database using mvc使用mvc从数据库中获取数据
【发布时间】:2014-04-10 08:13:20
【问题描述】:

我正在尝试从数据库中检索数据并将记录填充到表中,但我收到一个错误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) {这一行收到此错误

【问题讨论】:

  • 快看db.Movies
  • 什么是db?初始化了吗?
  • @Rex & Kjartan 我更新了我的问题
  • 尝试将 GetList Action 移动到您的控制器中
  • @fofik:它已经在控制器中,只是对齐不好。 :)

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


【解决方案1】:

这样试试,

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

【讨论】:

  • 这行得通,我不必在视图中放置@IF。但是,如果我只想从数据库中选择 FirstName、SecondName 和 DOB,该怎么办。那么我的模块会是什么样子?
  • @User911 @If 只是确保如果您的模型为空,那么它不会给您错误页面。如果您必须在页面右侧仅显示 FirstName、SecondName 和 DOB 字段?或者你只需​​要从数据库中检索这三个字段?
  • 我得到重复的结果,这是为什么?我不能按名称选择不同的记录吗?
  • @User911 它显示重复的结果,因为同名在数据库中存储了很多次。是的,您可以在查询中选择不同的记录。
  • 仅出于体验目的,如果我有每个记录的 ID,这就是我选择不同记录的方式 `var Movies = (from movie in db.Movies select movie).Distinct().ToList ();` 我会在哪里放置 ID 以便它根据 id 选择不同的记录?
【解决方案2】:

你在这里得到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);
    }
}

【讨论】:

  • 我收到错误:An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code Additional information: The entity or complex type 'Movie.Models.MovieModel' cannot be constructed in a LINQ to Entities query.
  • 第一次发布的答案与已接受的答案相同。 :)
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 2012-12-14
    • 2012-03-04
    • 2016-11-21
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多