我有以下数据库模式:
Equipment.cs:
public class Equipment
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[DisplayName("Serial Number")]
public string Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Amount { get; set; }
public int EquipmentStatusId { get; set; }
[ValidateNever]
public EquipmentStatus? EquipmentStatus { get; set; }
public int LocationId { get; set; }
[ValidateNever]
public Location? Location { get; set; }
[Required]
[DisplayName("Internal or Consigned")]
public string InternalOrConsigned { get; set; }
[DisplayName("EOL Date")]
public DateTime? EOLDate { get; set; }
public string? EOLDescription { get; set; }
[ValidateNever]
[DisplayName("Image")]
public string? ImageUrl { get; set; }
Location.cs:
public class Location
{
[Key]
public int Id { get; set; }
public int FloorId { get; set; }
[ValidateNever]
public Floor? Floor { get; set; }
public int RoomId { get; set; }
[ValidateNever]
public Room? Room { get; set; }
public int RackId { get; set; }
[ValidateNever]
public Rack? Rack { get; set; }
[Required]
public string Row { get; set; }
}
我希望能够在一个Create.cshtml
Equipments
视图中将所有属性添加到数据库中,我知道如何使用SelectListItem
对EquipmentStatusId
执行此操作,因为它只有一个名称,但当LocationId
具有多个属性并且它们也来自其他表时,我不确定如何对LocationId
执行此操作。
设备测试控制器. cs创建方法:
// GET: User/EquipmentsTest/Create
public IActionResult Create()
{
ViewData["EquipmentStatusId"] = new SelectList(_context.EquipmentStatuses, "Id", "Name");
ViewData["LocationId"] = new SelectList(_context.Locations, "Id", "Row");
return View();
}
// POST: User/EquipmentsTest/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Equipment equipment)
{
if (ModelState.IsValid)
{
_context.Add(equipment);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["EquipmentStatusId"] = new SelectList(_context.EquipmentStatuses, "Id", "Name", equipment.EquipmentStatusId);
ViewData["LocationId"] = new SelectList(_context.Locations, "Id", "Row", equipment.LocationId);
return View(equipment);
}
Create.cshtml:
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Id" class="control-label"></label>
<input asp-for="Id" class="form-control" />
<span asp-validation-for="Id" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Amount" class="control-label"></label>
<input asp-for="Amount" class="form-control" />
<span asp-validation-for="Amount" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EquipmentStatusId" class="control-label"></label>
<select asp-for="EquipmentStatusId" class ="form-control" asp-items="ViewBag.EquipmentStatusId"></select>
</div>
<div class="form-group">
<label asp-for="LocationId" class="control-label"></label>
<select asp-for="LocationId" class ="form-control" asp-items="ViewBag.LocationId"></select>
</div>
<div class="form-group">
<label asp-for="InternalOrConsigned" class="control-label"></label>
<input asp-for="InternalOrConsigned" class="form-control" />
<span asp-validation-for="InternalOrConsigned" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EOLDate" class="control-label"></label>
<input asp-for="EOLDate" class="form-control" />
<span asp-validation-for="EOLDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EOLDescription" class="control-label"></label>
<input asp-for="EOLDescription" class="form-control" />
<span asp-validation-for="EOLDescription" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ImageUrl" class="control-label"></label>
<input asp-for="ImageUrl" class="form-control" />
<span asp-validation-for="ImageUrl" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
1条答案
按热度按时间chy5wohz1#
你可以创建一个ViewModel来实现它,请参考这个简单的演示:
然后创建包含
Location
和Equipment
的视图模型:演示:
提交表格后:
数据将成功保存到数据库中: