MVC错误:未将对象引用设置到对象的实例 [英] MVC Error: Object reference not set to an instance of an object
问题描述
我接近这个MVC应用程序放弃今天!
I'm close to giving up on this mvc app for today!!
我遵循MVC音乐商店教程,我坚持54页
I'm following the Mvc Music Store Tutorial and I'm stuck on page 54.
这是我收到的错误:
System.NullReferenceException:对象
未设置为一个实例
对象。
System.NullReferenceException: Object reference not set to an instance of an object.
在以下code第三段块(DropDownList的)发生错误:
The error occurs in the third paragraph block (dropdownlist) in the following code:
<%@ 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文件中:
This ascx file is contained within an Edit.aspx file:
<%@ 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>
我知道有很多code的存在,但如果任何人都可以看到的东西很明显,我做错了,我会非常感激。
I realise there's a lot of code there but if anyone can see something obvious that I am doing wrong I'd be very grateful.
修改
StoreManagerController.cs(编辑)
StoreManagerController.cs (Edit)
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
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,这是一个错字,但一切都被相应标记。
Again I realise i called it MvcMovies1, this was a typo but everything is marked up accordingly.
推荐答案
确实专辑
有一个 ArtistId
自您正在呼叫 Model.ArtistId
,如果专辑
该行没有它的属性,你会得到一个空引用异常。这是因为该模型是该对象的速记是强类型,以你的观点,这恰好是专辑
你的情况。
Does Album
have an ArtistId
since in that line you are calling Model.ArtistId
and if Album
doesn't have that property on it you will get a null reference exception. That's because the Model is a shorthand for the object that is strongly typed to your view, which happens to be Album
in your case.
有在你上面code没有在那里你在哪里设置计算机[艺术家。你设置任何地方,因为这可能是你的问题了。
There is no where in your above code where you are setting the ViewData["Artists"]. Are you setting that anywhere since that could be your issue too.
修改
设置ViewData的的行动,它应该工作:
Set the ViewData in the action and it should work:
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);
}
这篇关于MVC错误:未将对象引用设置到对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!