MVC错误:未将对象引用设置为对象的实例

今天我已经接近放弃这个mvc应用了!!

我正在关注Mvc音乐商店教程,我被困在第54页。

这是我得到的错误:

System.NullReferenceException:未将对象引用设置为对象的实例。

错误发生在以下代码中的第三个段落(下拉列表)中:

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 

model.Title) %> model.Title) %> model.Title) %>

model.Price) %> model.Price) %> model.Price) %>

model.AlbumArtUrl) %> model.AlbumArtUrl) %> model.AlbumArtUrl) %>

model.Artist) %>

model.Genre) %>

此ascx文件包含在Edit.aspx文件中:

 <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>  Edit   

Edit

Edit Album model.Album, new { Artists = Model.Artists, Genres = Model.Genres }) %>

我意识到那里有很多代码,但是如果有人能够看到一些明显我做错的话,我会非常感激。

编辑

StoreManagerController.cs(编辑)

  public ActionResult Edit(int id) { var viewModel = new StoreManagerViewModel { Album = storeDB.Albums.SingleOrDefault(a => a.AlbumId == id), Genres = storeDB.Genres.ToList(), Artists = storeDB.Artists.ToList() }; return View(viewModel); } 

Andddd..StoreManagerViewModel.cs

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using MvcMovies1.Models; namespace MvcMovies1.ViewModels { public class StoreManagerViewModel { public Album Album { get; set; } public List Artists { get; set; } public List Genres { get; set; } } } 

我再次意识到我称之为MvcMovies1,这是一个错字,但所有内容都相应标记。

Album是否具有ArtistId因为在该行中您调用的是Model.ArtistId ,如果Album上没有该属性,您将获得空引用exception。 这是因为模型是对您的视图强类型的对象的简写,在您的情况下恰好是Album

您在上面的代码中没有设置ViewData [“Artists”]的位置。 您是否在任何地方设置,因为这也可能是您的问题。

编辑

在操作中设置ViewData,它应该工作:

 public ActionResult Edit(int id) { var viewModel = new StoreManagerViewModel { Album = storeDB.Albums.SingleOrDefault(a => a.AlbumId == id), Genres = storeDB.Genres.ToList(), Artists = storeDB.Artists.ToList() }; ViewData["Artists"] = storeDB.Artists.ToList(); ViewData["Genres"] = storeDB.Genres.ToList(); return View(viewModel); } 

首先,您需要在视图模型中添加属性以保存选定的艺术家和所选类型:

 public class StoreManagerViewModel { public Album Album { get; set; } public int? SelectedArtistId { get; set; } public List Artists { get; set; } public int? SelectedGenreId { get; set; } public List Genres { get; set; } } 

然后在Edit.aspx视图中而不是:

 <%: Html.EditorFor(model => model.Album, new { Artists = Model.Artists, Genres = Model.Genres }) %> 

你可以简单地说:

 <%: Html.EditorForModel() %> 

并在您的编辑器模板中~/Views/Home/EditorTemplates/Album.ascx

 <%@ Import Namespace ="MvcMovies1" %> <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 

<%: Html.LabelFor(model => model.Title) %> <%: Html.TextAreaFor(model => model.Title) %> <%: Html.ValidationMessageFor(model => model.Title) %>

<%: Html.LabelFor(model => model.Price) %> <%: Html.TextAreaFor(model => model.Price) %> <%: Html.ValidationMessageFor(model => model.Price) %>

<%: Html.LabelFor(model => model.AlbumArtUrl) %> <%: Html.TextAreaFor(model => model.AlbumArtUrl) %> <%: Html.ValidationMessageFor(model => model.AlbumArtUrl) %>

并在您的编辑器模板中~/Views/Home/EditorTemplates/StoreManagerViewModel

 <%@ Import Namespace ="MvcMovies1" %> <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <%: Html.EditorFor(model => model.Album) %> 

<%: Html.LabelFor(model => model.SelectedArtistId) %> <%: Html.DropDownListFor(model => model.SelectedArtistId, new SelectList(Model.Artists, "ArtistId", "Name")) %>

<%: Html.LabelFor(model => model.SelectedGenreId) %> <%: Html.DropDownListFor(model => model.SelectedGenreId, new SelectList(Model.Genres, "GenreId", "Name")) %>

<%: Html.ActionLink("Back to List", "Index") %>

您是否尝试过转换为List和List而不是IEnumerable?

你只是没有设置ViewData["Artists"]ViewData["genres"]正如我从你的动作方法中看到的那样。

如果你想在你的视图中使用它们,请确保在请求的生命周期中将这些值设置在某处

尝试运行迁移时出现相同的错误(update-database)。 有2个项目,即ViewProject和DBcontextProject。

我的问题是我使ViewProject成为一个启动项目并在DBcontextProject上运行迁移。

解决方案是使DBcontextProject成为一个启动项目并在DBcontextProject上运行迁移。

问题:

你调用的对象是空的

解:

 pubblic ActionResult yourDetails() { List PropertyName= new List(); list = db.PropertyName.ToList(); return View(list); } 

YourEntityName您的表名,即PassengerDetial

PropertyName您的类pubblic属性,即PassengerDetails

 pubblic Class PassengerDetailContext : DbContext { pubblic DbSet passegerDetails {get;set;} } List passegerDetails = new List(); list = db.passegerDetails.ToList(); return View(list);