Tôi sắp ngừng từ bỏ ứng dụng mvc này cho hôm nay !!Lỗi MVC: Tham chiếu đối tượng không được đặt thành phiên bản của đối tượng
tôi sau MVC Music Store Hướng dẫn và tôi bị mắc kẹt trên trang 54.
đây là lỗi tôi nhận được:
System.NullReferenceException: Object reference not set to an instance of an object.
Lỗi này xảy ra trong khối đoạn thứ ba (DropDownList) trong đoạn mã sau:
<%@ 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>
file ascx này được chứa trong một tập tin 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>
Tôi nhận ra có rất nhiều mã ở đó nhưng nếu có ai có thể thấy điều gì đó hiển nhiên rằng tôi đang làm sai, tôi sẽ rất biết ơn.
EDIT
StoreManagerController.cs (Chỉnh sửa)
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; }
}
}
Một lần nữa tôi nhận ra tôi gọi nó là MvcMovies1, đây là một lỗi đánh máy nhưng mọi thứ đều được đánh dấu tương ứng.
Bạn cũng có thể đăng tác vụ trong bộ điều khiển của mình không. Bạn có thiết lập ViewData ["Artists"] trong bộ điều khiển không? – lancscoder
Xin chào Simon G. Tôi đã đăng mã được yêu cầu. Cảm ơn vi đa trả lơi. – 109221793
Bạn có thể đặt điểm ngắt trên dòng đó và kiểm tra tất cả các thuộc tính cho null không? – Rup