您好,我想设置我的应用程序,以便在 PareSubgroup 表中创建新字段时,在我的选择字段中的视图中,创建时,它返回的不是 Schedule 表中的 id,而是主题表中的学科名称。
public IActionResult Create()
{
ViewData["Pare_Id"] = new SelectList(_context.Schedules, "Id", "Id");
ViewData["Subgroup_Id"] = new SelectList(_context.Subgroups, "Id", "Id");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Pare_Id,Subgroup_Id")] PareSubgroup pareSubgroup)
{
if (ModelState.IsValid)
{
_context.Add(pareSubgroup);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["Pare_Id"] = new SelectList(_context.Schedules, "Id", "Id", pareSubgroup.Pare_Id);
ViewData["Subgroup_Id"] = new SelectList(_context.Subgroups, "Id", "Id", pareSubgroup.Subgroup_Id);
return View(pareSubgroup);
}
@{ ViewData["Title"] = "Create"; }
<h1>Create</h1>
<h4>PareSubgroup</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Pare_Id" class="control-label"></label>
<select asp-for="Pare_Id" class="form-control" asp-items="ViewBag.Pare_Id"></select>
</div>
<div class="form-group">
<label asp-for="Subgroup_Id" class="control-label"></label>
<select asp-for="Subgroup_Id" class="form-control" asp-items="ViewBag.Subgroup_Id"></select>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }