gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:57:31 24 4
gpt4 key购买 nike

今天我差点放弃这个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com