我遇到以下问题,我创建了一个应用程序,将游戏类别和游戏本身添加到数据库中。我创建了一个关系,但是不幸的是,当我添加到数据库中时,我得到了一个错误。
模型绑定的复杂类型不能为抽象或值类型,并且必须具有无参数的构造函数。
游戏类别模型
using System.Collections.Generic;
namespace relationship.Models
{
public class GameCategory
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Game> Game { get; set; }
}
}
游戏模型
namespace relationship.Models
{
public class Game
{
public int GameId { get; set; }
public string Name { get; set; }
public GameCategory Category { get; set; }
public int CategoryId { get; set; }
}
}
视图模型
using relationship.Models;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace relationship.ViewModels
{
public class AddGameViewModel
{
[Required]
public string GameName { get; set; }
public int CategoryID { get; set; }
public List<SelectListItem> Categories { get; set; }
public AddGameViewModel(IEnumerable<GameCategory> categories)
{
Categories = new List<SelectListItem>();
foreach (var catData in categories)
{
Categories.Add(new SelectListItem { Text = catData.Name.ToString(), Value = catData.Id.ToString() });
}
return;
}
}
}
游戏库
using System.Collections.Generic;
using System.Linq;
namespace relationship.Models
{
public class GameRepository : IGameRepository
{
private readonly AppDbContext appDbContext;
public GameRepository(AppDbContext dbContext)
{
appDbContext = dbContext;
}
public void AddGame(Game game)
{
appDbContext.Games.Add(game);
appDbContext.SaveChanges();
}
public IEnumerable<Game> Games()
{
return appDbContext.Games.ToList();
}
}
}
最后是GameController
using Microsoft.AspNetCore.Mvc;
using relationship.Models;
using relationship.ViewModels;
namespace relationship.Controllers
{
public class GameController : Controller
{
private readonly IGameRepository gameRepository;
private readonly ICategoryRepository categoryRepository;
public GameController(IGameRepository gameRepo, ICategoryRepository catRepo)
{
gameRepository = gameRepo;
categoryRepository = catRepo;
}
public IActionResult Index()
{
return View();
}
[HttpGet]
public IActionResult Add()
{
var addGameViewModel = new AddGameViewModel(categoryRepository.GameCategory());
return View(addGameViewModel);
}
[HttpPost]
public IActionResult Add(AddGameViewModel addGameViewModel)
{
if (ModelState.IsValid)
{
GameCategory gameCategory = categoryRepository.GetDetails(addGameViewModel.CategoryID);
if(gameCategory == null)
{
return NotFound();
}
Game game = new Game
{
Name = addGameViewModel.GameName,
Category = gameCategory
};
gameRepository.AddGame(game);
return RedirectToAction("Index");
}
return View(addGameViewModel);
}
}
}
我不知道怎么了。
我的错误画面
无法创建Relationship.ViewModels.AddGameViewModel的实例。模型绑定的复杂类型不能为抽象或值类型,并且必须具有无参数的构造函数。
让我们尝试解决此错误。
无法创建Relationship.ViewModels.AddGameViewModel的实例。
不言而喻:模型绑定组件试图创建您类型的实例,但是失败了。
模型绑定的复杂类型
“模型绑定”是指它们被ASP.NET管道绑定。“复杂类型”基本上是不是“基本”如string
或的任何类型int
。您的模型类是复杂的类型。
一定不能抽象
模型绑定系统将希望能够创建类的实例,因此它不能是抽象的。它必须是具体的。您显示的所有类型都是具体的,所以这不是问题。
或值类型
您不能将struct
类型与模型绑定一起使用;这只是其局限性之一。幸运的是,您的类型都是所有类,因此您可以忽略它。
并且必须具有无参数的构造函数。
ASP.NET不知道如何向模型构造函数提供参数。它只能做的等效项new T()
,因此所有模型类型都必须定义一个具有零参数的构造函数。这就是您看到错误的原因;您的AddGameViewModel
类仅定义此构造函数:
public AddGameViewModel(IEnumerable<GameCategory> categories)
C#语言的功能之一是,当您不手动指定构造函数时,它将为您添加一个默认的构造函数。当您在代码中定义构造函数时,不会添加此默认构造函数。
在所有其他模型中,您没有定义任何构造函数,因此编译器会为您添加默认构造函数。如果AddGameViewModel
您已添加构造函数,则要解决此问题,还必须添加默认构造函数:
public AddGameViewModel()
{
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句