我需要为数据库更新创建一个空表单。在控制器中,这称为: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'.
建议?
你需要从@model DEIGList.Models.MeetingListContext
改变
到@model DEIGList.Models.List
@model的类型必须与View("Edit", new List())
中的模型类型相同
更新:
如果您希望下载日期和时间,则需要更新控制器:
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"))
你的帖子指出了我的解决方案。解决方案是使用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());