我创建两个模型,汽车制造和汽车品牌
在汽车品牌中,添加BrandID并使用ID连接这两个Model
。
public virtual MarkeVozila MarkaVozila { get; set; }
此后,我使用ID
在Create,Edit和Index Action方法中通过控制器中的ViewBag
控制器
public IActionResult Index()
{
return View(_db.Modeli.Include(m =>m.MarkaVozila).ToList());
}
//GET Create Action Metod
public IActionResult Create()
{
ViewBag.MarkaVozilaId = new SelectList(_db.MarkeVozila, "Id", "Ime");
return View();
}
//POST Create Action Metod
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(ModeliVozila modeli)
{
if (ModelState.IsValid)
{
_db.Add(modeli);
await _db.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewBag.MarkaVozilaId = new SelectList(_db.MarkeVozila, "Id", "Ime", modeli.MarkaVozila.Id);
return View(modeli);
}
//GET Edit Action Metod
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var modeliVozila = await _db.Modeli.FindAsync(id);
if (modeliVozila == null)
{
return NotFound();
}
ViewBag.MarkaVozilaId = new SelectList(_db.MarkeVozila, "Id", "Ime");
return View(modeliVozila);
}
//POST Edit Action Metod
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, ModeliVozila modeliVozila)
{
if (id != modeliVozila.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
_db.Update(modeliVozila);
await _db.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewBag.MarkaVozilaId = new SelectList(_db.MarkeVozila, "Id", "Ime");
return View(modeliVozila);
}
使用ViewBag.MarkaVozilaId
我在View
下拉列表中创建的Aftrer显示了所有品牌的列表:例如“ Audi”,“ BMW”,“ VW”
<div class="form-group row">
<div class="col-2">
<label>Marka Vozila</label>
</div>
<div class="col-5">
<select asp-for="MarkaVozila" asp-items="ViewBag.MarkaVozilaId" class="form-control">
<option>Marka Vozila</option>
</select>
</div>
</div>
并且在“索引视图”中,我添加了一个表字段来显示“汽车品牌”,但是在我将汽车品牌分配给模型后,索引没有显示“汽车品牌”。
@model IEnumerable<Project.Models.ModeliVozila>
@{
ViewData["Title"] = "Index";
}
<br /><br />
<div class="row">
<div class="col-6">
<h2 class="text-info">Modeli Vozila</h2>
</div>
</div>
<br />
<div>
<table class="table table-striped border datatable-modeliVozila">
<thead>
<tr class="bg-info">
<th>Modela vozila</th>
<th>Marke vozila</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(m => item.Ime)
</td>
<td>
@Html.DisplayFor(m => item.MarkaVozila.Ime)
</td>
<td>
<partial name="_TableButtonPartial" model="item.Id" />
</td>
</tr>
}
</tbody>
</table>
</div>
有人知道我在哪里犯错了吗?
MarkeVozila模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace TriglavOsiguranje.Models
{
public class MarkeVozila
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(30)]
public string Ime { get; set; }
}
}
**ModeliVozila Model**
using System.ComponentModel.DataAnnotations;
namespace TriglavOsiguranje.Models
{
public class ModeliVozila
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(20)]
public string Ime { get; set; }
public virtual MarkeVozila MarkaVozila { get; set; }
}
}