在使用DropDown和ListBox列表尝试创建对象时遇到此错误
System.NullReferenceException System.Web.Mvc.WebViewPage.Model.get返回null。
型号
public class Game
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
public Publisher Publishers { get; set; }
public int PublisherID { get; set; }
public List<Category> Categories { get; set; }
public string ImageLink { get; set; }
public Game()
{
Categories = new List<Category>();
}
}
查看模型
public class GameViewModel
{
public int Id { get; set; }
public string Title { get; set; }
public object Category { get; set; }
public Publisher Publisher { get; set; }
public string ImageLink { get; set; }
public GameViewModel(int id, string title, List<Category> category, Publisher publisher, string imageLink)
{
this.ID = id;
this.Title = title;
this.Category = category;
this.Publisher = publisher;
this.ImageLink = imageLink;
}
创建视图模型
public class CreateGameViewModel{
public Game Game { get; private set; }
public List<SelectListItem> Categories { get; private set; }
public List<SelectListItem> Publishers { get; private set; }
public int[] SelectedCategory { get; set; }
public CreateGameViewModel()
{
this.Game = new Game();
}
public CreateGameViewModel(List<Models.Category> categories, List<Models.Publisher> publishers)
{
Categories = categories.Select(c => new SelectListItem() { Text = c.Name, Value = c.Id.ToString() }).ToList();
Publishers = publishers.Select(p => new SelectListItem() { Text = p.Name, Value = p.Id.ToString() }).ToList();
}
}
我的控制器
GET
public ActionResult Create()
{
var publishers = _db.Publishers.ToList();
var categories = _db.Categories.ToList();
var gameVM = new CreateGameViewModel(categories: categories, publishers: publishers);
return View(gameVM);
}
POST
[HttpPost]
public ActionResult Create(CreateGameViewModel game)
{
try
{
var categories = _db.Categories.Where(c => game.SelectedCategory.Contains(c.Id)).ToList();
game.Game.Categories = categories;
_db.Games.Add(game.Game);
_db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
我的观点(出现错误的地方)
@Html.DropDownListFor(m => m.Game.PublisherID, Model.Publishers)
@Html.ListBoxFor(m => m.SelectedCategory, Model.Categories)
在此创建视图中,我还同时具有ID和PublisherID的编辑器,这肯定是不正确的,但是我不确定在其他问题存在的情况下该问题的确切位置。
有人可以看一下这段代码吗?
答案 0 :(得分:0)
所以我认为您的问题是Game
尚未定义,因此当您尝试执行@Html.DropDownListFor(m => m.Game.PublisherID, Model.Publishers)
时,游戏为null会导致错误
您的代码:
public class CreateGameViewModel
{
public Game Game { get; private set; }
public List<SelectListItem> Categories { get; private set; }
public List<SelectListItem> Publishers { get; private set; }
public int[] SelectedCategory { get; set; }
public CreateGameViewModel()
{
this.Game = new Game();
}
public CreateGameViewModel(List<Models.Category> categories,
List<Models.Publisher> publishers)
{
Categories = categories.Select(c => new SelectListItem() { Text = c.Name,
Value = c.Id.ToString() }).ToList();
Publishers = publishers.Select(p => new SelectListItem() { Text = p.Name,
Value = p.Id.ToString() }).ToList();
}
}
修复:
public class CreateGameViewModel
{
public Game Game { get; private set; }
public List<SelectListItem> Categories { get; private set; }
public List<SelectListItem> Publishers { get; private set; }
public int[] SelectedCategory { get; set; }
public CreateGameViewModel()
{
this.Game = new Game();
}
public CreateGameViewModel(List<Models.Category> categories,
List<Models.Publisher> publishers)
:this()
{
Categories = categories.Select(c => new SelectListItem() { Text = c.Name,
Value = c.Id.ToString() }).ToList();
Publishers = publishers.Select(p => new SelectListItem() { Text = p.Name,
Value = p.Id.ToString() }).ToList();
}
}
次要差异是第二个构造函数中的:this(),这应调用第一个构造函数初始化Game
,这应避免发生错误
希望这会有所帮助