我需要为数据库更新创建一个空表格。在控制器中,这称为:public ViewResult Create() => View("Edit", new List());
<TargetFramework>netcoreapp2.2</TargetFramework>
I have a class MeetingListContext : DbContext that has 3 models:
1. DbSet List
2. DbSet ATime (Look up table to show time name instead of time id)
3. DbSet DOW (Look up table to show day name instead of day id)
型号
namespace DEIGList.Models
{
public partial class MeetingListContext : DbContext
{
public MeetingListContext()
{
}
public MeetingListContext(DbContextOptions<MeetingListContext> options)
: base(options)
{
}
public virtual DbSet<ATime> ATime { get; set; }
public virtual DbSet<Dow> Dow { get; set; }
public virtual DbSet<List> List { get; set; }
视图:
@model DEIGList.Models.MeetingListContext
在控制器中:
public ViewResult Create() => View("Edit", new List());
预期结果将是一个带有空白数据字段的表单,用于填充数据。 我希望能够在日期和时间中使用下拉菜单(查找)。
但是当我使用@model DEIGList.Models.MeetingListContext
时,我得到了:
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'DEIGList.Models.List', but this ViewDataDictionary instance requires a model item of type 'DEIGList.Models.MeetingListContext'.
建议?
答案 0 :(得分:0)
您需要从@model DEIGList.Models.MeetingListContext
更改
到@model DEIGList.Models.List
@model的类型必须与View("Edit", new List())
中的模型类型相同
更新:
如果您想下拉日期和时间,则需要更新Controller:
public ViewResult Create() {
MeetingListContext ctx = new MeetingListContext();
ViewBag.ATime = ctx.ATime.ToList();
ViewBag.Dow = ctx.Dow.ToList();
return View("Edit", new List());
}
在视图中添加下拉帮助器:
@Html.DropDownList("ATime", new SelectList(ViewBag.ATime, "ATimeID", "ATimeName"))
@Html.DropDownList("Dow", new SelectList(ViewBag.Dow, "DowID", "DowName"))
答案 1 :(得分:0)
您的帖子向我提出了解决方案。解决方案是使用id == 0进行选择。
var list = _context.List
.Where(l => l.ListId == 0)
.Include(t => t.ATime)
.Include(d => d.Dow)
.ToList();
ViewBag.thelist = list;
return View("Edit", new List());