[英]Inheritance Database in Entity Framework Core and ASP.NET Core MVC
[英]Asp.Net core MVC Microsoft Entity Framework
我的 MVC asp.net 核心站点有一个问题。 我想从数据库中获取数据,然后将它们粘贴到 combobox 中。 我真的不知道是什么导致出现此错误消息:错误消息
我的创业
services.AddDbContext<ConnectionStringClass>(options => options.UseSqlServer(Configuration.GetConnectionString("GETDATA"))); //Stored in json
/Views/Home 中的我的 Index.cshtml
@{
ViewData["Title"] = "Home Page";
}
<select id="Select1" asp-items="@(new SelectList(Model.displaydata,"Id","CisloReklamace"))">
<options value="" selected disabled>
</options>
</select>
我的 HomeController 在 /Views/Home
private readonly ILogger<HomeController> _logger;
private readonly ConnectionStringClass _db;
public HomeController(ILogger<HomeController> logger, ConnectionStringClass db)
{
_logger = logger;
_db = db;
}
public IEnumerable<EmpClass> displaydata { get; set; }
public async Task OnGet()
{
displaydata = await _db.Reklamace.ToListAsync();
}
public IActionResult Index()
{
var model = new HomeController(_logger, _db);
return View(model);
}
public IActionResult Privacy()
{
return View();
}
[AllowAnonymous]
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
我的 EmpClass in.Models
public class EmpClass
{
[Key]
public int Id { get; set; }
public string CisloReklamace { get; set; }
public string IdAdresa { get; set; }
public string Zakazka { get; set; }
}
我的 ConnectionStringClass in.Models
public class ConnectionStringClass: DbContext
{
public ConnectionStringClass(DbContextOptions<ConnectionStringClass> options): base(options)
{
}
public DbSet<EmpClass> Reklamace { get; set; }
}
谢谢你的帮助。
Controller
public async Task<IActionResult> Index()
{
var model = await _db.Reklamace.ToListAsync();
return View(model);
}
看法
@model IEnumerable<EmpClass>
<select id="Select1" asp-items="@(new SelectList(Model.displaydata,"Id","CisloReklamace"))">
<options value="" selected disabled>
</options>
</select>
在 controller 中,您通过 model 查看@model IEnumerable<EmpClass>
。 在您的代码中,您尝试将 model 作为 HomeController 传递。 所以你的视图不能绑定 model 并且你得到 error = null 异常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.