C# MVC5中绑定到复杂模型的控制器
我有一个模型(a)由另外两个模型组成(B-超声波DTO,C-血液测试DTO)。 当我尝试通过带有视图的MVC控制器添加控制器时,使用VS2017中的Entity Framework选项,它会创建带有相应视图的控制器,尽管它只绑定模型a的属性,而不绑定B和C的属性。有没有办法解决这个问题,或者我必须自己把剩下的绑起来 [控制器创建选项][1]C# MVC5中绑定到复杂模型的控制器,c#,asp.net-mvc,scaffolding,C#,Asp.net Mvc,Scaffolding,我有一个模型(a)由另外两个模型组成(B-超声波DTO,C-血液测试DTO)。 当我尝试通过带有视图的MVC控制器添加控制器时,使用VS2017中的Entity Framework选项,它会创建带有相应视图的控制器,尽管它只绑定模型a的属性,而不绑定B和C的属性。有没有办法解决这个问题,或者我必须自己把剩下的绑起来 [控制器创建选项][1] using System; using System.Collections.Generic; using System.ComponentModel.Da
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
public class CheckUpDTO
{
public int Id { get; set; }
public int DayOfPeriod { get; set; }
[Required]
public UltrasoundDTO MyUltrasoundDTO { get; set; }
[Required]
public BloodTestDTO MyBloodTestDTO { get; set; }
}
控制器中的一种方法:
public async Task<IActionResult> Create([Bind("Id,DayOfPeriod")] CheckUpDTO checkUpDTO)
{
if (ModelState.IsValid)
{
_context.Add(checkUpDTO);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(checkUpDTO);
}
public async Task Create([Bind(“Id,dayoff period”)]CheckUpDTO CheckUpDTO)
{
if(ModelState.IsValid)
{
_添加(checkUpDTO);
wait_context.SaveChangesAsync();
返回操作(“索引”);
}
返回视图(checkUpDTO);
}
与上述方法相对应的视图:
@model DTOs.CheckUpDTO
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<form asp-action="Create">
<div class="form-horizontal">
<h4>CheckUpDTO</h4>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="DayOfPeriod" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="DayOfPeriod" class="form-control" />
<span asp-validation-for="DayOfPeriod" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</form>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
@model DTOs.CheckUpDTO
@{
ViewData[“标题”]=“创建”;
}
创造
检查
返回列表
@节脚本{
@{wait Html.RenderPartialAsync(“_validationScript”);}
}