我相信我有正确的方法但是,我无法让它发挥作用。我正在使用代码第一种方法。我有三张桌子。接下来,我为Build-Inspection创建了一个视图模型。根据类别是否包装或检查,检查和包装表都是可插入的可选表格。我的观点不会显示。为什么会这样?为什么我没有错误?
public class Build
{
public int BuildID { get; set; }
// order of each individual step
public int BuildStep { get; set; }
public string Category { get; set; }
public string SubCategory { get; set; }
public string Details { get; set; }
public string AdditionalNotes { get; set; }
public DateTime? Date { get; set; }
public int? Weight { get; set; }
public Inspection Inspection { get; set; }
public Packaging Packaging { get; set; }
}
包装表
public class Packaging
{
public int PackagingID { get; set; }
public int MaxWeight { get; set; }
public int? MINWeight { get; set; }
public int? MAXWidth { get; set; }
public int? MaxHeight { get; set; }
public int BuildID {get; set;}
public Build Build { get; set; }
}
检查表
public class Inspection
{
public int InspectionID {get; set;}
public int Type {get; set;}
public int TensileMAX {get; set;}
public int BuildID {get; set;}
public Build Build { get; set; }
}
查看构建检查模型
public class BuildInspectionViewModel
{
public int BuildStep { get; set; }
public string Category { get; set; }
public string SubCategory { get; set; }
public string Details { get; set; }
public string AdditionalNotes { get; set; }
public DateTime? Date { get; set; }
public int? Weight { get; set; }
public int Type {get; set;}
public int TensileMAX {get; set;}
}
接下来我制作了一个控制器,它应该能够使用视图
插入到两个表中 //grabbing my db context or my database
private readonly DataContext _context;
public BuildController(DataContext context)
{
_context = context;
}
[HttpPost]
public ActionResult Create(BuildPackagingViewModel viewModel)
{
Build build = new Build()
{
BuildStep = viewModel.BuildStep,
Category = viewModel.Category,
SubCategory = viewModel.SubCategory,
Details = viewModel.Details,
AdditionalNotes = viewModel.AdditionalNotes
};
Inspection inspection = new Inspection()
{
Type = viewModel.Type,
TensileMax = viewModel.TensileMax,
};
_context.Build.Add(build);
inspection.BuildID = build.BuildID;
_context.Inspection.Add(inspection);
_context.SaveChanges();
return View();
然后我做了一个视图,这样我就可以插入检查和构建表。
@model DemoOnMVVM.Models.ViewModels.BuildInspectionViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<form asp-controller="BuildInspection" asp-action="Create" method="post"
class="form-horizontal" role="form">
<fieldset>
<legend> BuildInspectionViewModel </legend>
<div class="editor-label">
@Html.LabelFor(model => model.BuildStep)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.BuildStep)
@Html.ValidationMessageFor(model => model.BuildStep)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Category)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Category)
@Html.ValidationMessageFor(model => model.Category)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.SubCategory)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SubCategory)
@Html.ValidationMessageFor(model => model.SubCategory)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Details)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Details)
@Html.ValidationMessageFor(model => model.Details)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AdditionalNotes)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AdditionalNotes)
@Html.ValidationMessageFor(model => model.AdditionalNotes)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Weight)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Weight)
@Html.ValidationMessageFor(model => model.Weight)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Type)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Type)
@Html.ValidationMessageFor(model => model.Type)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TensileMax)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TensileMax)
@Html.ValidationMessageFor(model => model.TensileMax)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
这是我用来搭建数据库的DataContext
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
}
public DbSet<Packaging> Packaging { get; set; }
public DbSet<Build> Build { get; set; }
public DbSet<Inspection> Inspection { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Inspection>().ToTable("Inspection");
modelBuilder.Entity<Build>().ToTable("Build");
modelBuilder.Entity<Packaging>().ToTable("Packaging");
}
}
这是我的startup.cs文件
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<DataContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DataContext")));
appsettings.json文件
{
"ConnectionStrings": {
"DataContext": "Mysqlserverconnection"
},
总的来说,我不确定为什么我的观点不会显示。我只使用代码第一种方法搭建包装,构建和检查表。以下是我的资源链接。
(Best way to insert data to multiple table MVC ASP)
(https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext)