c# - MVC 错误 : Object reference not set to an instance of an object

标签 c# asp.net-mvc

今天我差点放弃这个 mvc 应用!!

我正在学习 Mvc 音乐商店教程,但我卡在了第 54 页。

这是我遇到的错误:

System.NullReferenceException: Object reference not set to an instance of an object.

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

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

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

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

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

<p>
    <%: Html.LabelFor(model => model.Artist) %>
    <%: Html.DropDownList("ArtistId", new SelectList(ViewData["Artists"] as IEnumerable, "ArtistId", "Name", Model.ArtistId)) %>
</p>

<p>
    <%: Html.LabelFor(model => model.Genre) %>
    <%: Html.DropDownList("GenreId", new SelectList(ViewData["Genres"] as IEnumerable, "GenreId", "Name", Model.GenreId)) %>
</p>

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

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

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcMovies1.ViewModels.StoreManagerViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Edit
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<form id="form1" runat="server">
    <h2>Edit</h2>
    <% using (Html.BeginForm())
       { %>
      <%: Html.ValidationSummary(true)%>

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

          <p><input type="submit" value="Save" /></p>


    </fieldset>

      <% } %>
      </form>
</asp:Content>

我知道那里有很多代码,但如果有人能看出我做错了什么,我将不胜感激。

编辑

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<Artist> Artists { get; set; }
        public List<Genre> Genres { get; set; }
    }
}

我再次意识到我将其命名为 MvcMovies1,这是一个打字错误,但所有内容都已相应标记。

最佳答案

Album 是否有 ArtistId 因为在那一行你调用了 Model.ArtistId 并且如果 Album 没有' 拥有该属性,您将获得空引用异常。这是因为模型是强类型化到您的 View 的对象的简写,在您的情况下恰好是 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);
 }

关于c# - MVC 错误 : Object reference not set to an instance of an object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3979774/

相关文章:

c# - LMAX Disruptor 可以移植到 C# 吗?

C# Sql LocalDB 应用程序性能缓慢

c# - Controller 单元测试期间未启用角色管理器功能错误

asp.net - 从 asp.net mvc 下拉列表中获取值

c# - 组合框和 Entity Framework

c# - 区分卸载/安装和升级应用程序

c# - 如何使用 visual studio 命令提示符从 C# 类文件生成 xsd?

C# - 如何打印出 View 数据?

.net - 什么是 System.Web.Mvc.MvcHandler.ProcessAsyncRequest()?

c# - 使用 MVC 6 显示从 ASP.NET 5 中的 WebRequest 获取的 JSON 数据