具有不同 Sql Table 的动态下拉列表作为视图 MVC C#
Dynamic Dropdown with different Sql Table as the View MVC C#
我有一个 SQL 服务器 table 作为 CRUD 命令的来源。
table2_ID 列来自 table2(外键)。
现在,在编辑和创建 Table1 条目时,我想要 Table1 中的 table_ID
列的下拉列表,选项为:Table2 的 Name
,然后必须将 ID
保存为值。
控制器中从 Table2 获取数据(名称和 ID)的代码是什么样的?
以及在视图中将数据显示为下拉列表并将名称作为选项并将 ID 作为值的代码是什么样的?
表 1:
number
table2_ID
date
11
2
12.12.2021
32
3
13.12.2021
表2
ID
name
2
axis
3
bot
/ GET: Table1/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var table1 = await _context.Table1.FindAsync(id);
if (table1 == null)
{
return NotFound();
}
return View(table1);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("number, table2_ID, date")] Table1 table1)
{
if (ModelState.IsValid)
{
_context.Add(table1);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(table1);
}
//Create view
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div class="form-group">
<label asp-for="number" class="control-label"></label>
<input asp-for="number" class="form-control" />
<span asp-validation-for="number" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="table2_ID" class="control-label"></label>
<input asp-for="table2_ID" class="form-control" />
<span asp-validation-for="table2_ID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="date" class="control-label"></label>
<input asp-for="date" class="form-control" />
<span asp-validation-for="date" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
What does the code in the controller look like to get the data from
Table2 (name and ID)?
您可以得到如下表2的数据:
var data = _context.Table2.ToList();
and what does the code look like in the view to show the data as a
dropdown with name as an option and id as a value
您需要先将下拉列表值存储在 ViewBag
:
ViewBag.table2= new SelectList(data,"ID","name");
并使用如下 ViewBag
:
<select asp-for="Table2_ID" asp-items="@ViewBag.table2" class="form-control">
<option selected disabled value="">--- Choose ---</option>
</select>
整个工作演示:
型号:
public class Table1 {
public int ID { get; set; }
public DateTime date { get; set; }
public string Table2_ID { get; set; }
}
public class Table2 {
public int ID { get; set; }
public string name { get; set; }
}
查看:
@model Table1
<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="date" class="control-label"></label>
<input asp-for="date" class="form-control" />
<span asp-validation-for="date" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Table2_ID" class="control-label"></label>
<select asp-for="Table2_ID" asp-items="@ViewBag.table2" class="form-control">
<option selected disabled value="">--- Choose ---</option>
</select>
<span asp-validation-for="Table2_ID" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
控制器:
// GET: Table1/Create
public IActionResult Create()
{
ViewBag.table2 = new SelectList(_context.Table2.ToList(), "ID", "name");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,date,Table2_ID")] Table1 table1)
{
if (ModelState.IsValid)
{
_context.Add(table1);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewBag.table2 = new SelectList(_context.Table2.ToList(), "ID", "name",table1.Table2_ID);
return View(table1);
}
数据库上下文:
public class MyContext : DbContext
{
public MyContext(DbContextOptions<MyContext> options)
: base(options)
{
}
public DbSet<Table1> Table1 { get; set; }
public DbSet<Table2> Table2 { get; set; }
}
结果:
我有一个 SQL 服务器 table 作为 CRUD 命令的来源。
table2_ID 列来自 table2(外键)。
现在,在编辑和创建 Table1 条目时,我想要 Table1 中的 table_ID
列的下拉列表,选项为:Table2 的 Name
,然后必须将 ID
保存为值。
控制器中从 Table2 获取数据(名称和 ID)的代码是什么样的? 以及在视图中将数据显示为下拉列表并将名称作为选项并将 ID 作为值的代码是什么样的?
表 1:
number | table2_ID | date |
---|---|---|
11 | 2 | 12.12.2021 |
32 | 3 | 13.12.2021 |
表2
ID | name |
---|---|
2 | axis |
3 | bot |
/ GET: Table1/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var table1 = await _context.Table1.FindAsync(id);
if (table1 == null)
{
return NotFound();
}
return View(table1);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("number, table2_ID, date")] Table1 table1)
{
if (ModelState.IsValid)
{
_context.Add(table1);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(table1);
}
//Create view
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div class="form-group">
<label asp-for="number" class="control-label"></label>
<input asp-for="number" class="form-control" />
<span asp-validation-for="number" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="table2_ID" class="control-label"></label>
<input asp-for="table2_ID" class="form-control" />
<span asp-validation-for="table2_ID" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="date" class="control-label"></label>
<input asp-for="date" class="form-control" />
<span asp-validation-for="date" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
What does the code in the controller look like to get the data from Table2 (name and ID)?
您可以得到如下表2的数据:
var data = _context.Table2.ToList();
and what does the code look like in the view to show the data as a dropdown with name as an option and id as a value
您需要先将下拉列表值存储在 ViewBag
:
ViewBag.table2= new SelectList(data,"ID","name");
并使用如下 ViewBag
:
<select asp-for="Table2_ID" asp-items="@ViewBag.table2" class="form-control">
<option selected disabled value="">--- Choose ---</option>
</select>
整个工作演示:
型号:
public class Table1 {
public int ID { get; set; }
public DateTime date { get; set; }
public string Table2_ID { get; set; }
}
public class Table2 {
public int ID { get; set; }
public string name { get; set; }
}
查看:
@model Table1
<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="date" class="control-label"></label>
<input asp-for="date" class="form-control" />
<span asp-validation-for="date" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Table2_ID" class="control-label"></label>
<select asp-for="Table2_ID" asp-items="@ViewBag.table2" class="form-control">
<option selected disabled value="">--- Choose ---</option>
</select>
<span asp-validation-for="Table2_ID" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
控制器:
// GET: Table1/Create
public IActionResult Create()
{
ViewBag.table2 = new SelectList(_context.Table2.ToList(), "ID", "name");
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,date,Table2_ID")] Table1 table1)
{
if (ModelState.IsValid)
{
_context.Add(table1);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewBag.table2 = new SelectList(_context.Table2.ToList(), "ID", "name",table1.Table2_ID);
return View(table1);
}
数据库上下文:
public class MyContext : DbContext
{
public MyContext(DbContextOptions<MyContext> options)
: base(options)
{
}
public DbSet<Table1> Table1 { get; set; }
public DbSet<Table2> Table2 { get; set; }
}
结果: