With this example, it demonstrate all relational field between Primary Key and Foreign Key in order to retrieve the ID for DropDownList, I have an example below:
// From the controller of each class
public class TitlesController : Controller { private readonly ApplicationDbContext _context; public IEnumerable<SelectListItem> Authors { get; set; } public IEnumerable<SelectListItem> Categories { get; set; } public IEnumerable<SelectListItem> Publishers { get; set; } public TitlesController(ApplicationDbContext context) { _context = context; } // GET: Titles [Authorize(Roles = "admin")] public IActionResult Index() { var applicationDbContext = _context.Titles.Include(t => t.Authors).Include(t => t.Categories).Include(t => t.Publishers); return View(applicationDbContext.ToList()); } // GET: Titles/Details/5 [Authorize(Roles = "admin")] public IActionResult Details(int? id) { if (id == null) { return HttpNotFound(); } Titles titles = _context.Titles.Single(m => m.TitleId == id); if (titles == null) { return HttpNotFound(); } return View(titles); } private IEnumerable<SelectListItem> GetAllAuthors() { return _context.Authors.ToList().Select(authors => new SelectListItem { Text = authors.AuthorId.ToString(), Value = authors.AuthorId.ToString(), }); } private IEnumerable<SelectListItem> GetAllCategories() { return _context.Categories.ToList().Select(categories => new SelectListItem { Text = categories.CatId.ToString(), Value = categories.CatId.ToString(), //Text = categories.CatName.ToString(), //Value = categories.CatName.ToString(), }); } private IEnumerable<SelectListItem> GetAllPublishers() { return _context.Publishers.ToList().Select(publishers => new SelectListItem { Text = publishers.PubId.ToString(), Value = publishers.PubId.ToString(), }); } // GET: Titles/Create [Authorize(Roles = "admin")] public IActionResult Create() { // Get all Authors Id in DropDownList Authors = GetAllAuthors(); ViewBag.Authors = Authors; //ViewData["AuthorId"] = new SelectList(_context.Authors, "AuthorId", "Authors"); // Get all Categories Id in DropDownList Categories = GetAllCategories(); ViewBag.Categories = Categories; //ViewData["CatId"] = new SelectList(_context.Categories, "CatId", "Categories"); // Get all Categories Id in DropDownList Publishers = GetAllPublishers(); ViewBag.Publishers = Publishers; //ViewData["PubId"] = new SelectList(_context.Publishers, "PubId", "Publishers"); return View(); } // POST: Titles/Create [HttpPost] [ValidateAntiForgeryToken] [Authorize(Roles = "admin")] public IActionResult Create(Titles titles) { if (ModelState.IsValid) { _context.Titles.Add(titles); _context.SaveChanges(); return RedirectToAction("Index"); } // Get all Authors Id in DropDownList Authors = GetAllAuthors(); ViewBag.Authors = Authors; //ViewData["AuthorId"] = new SelectList(_context.Authors, "AuthorId", "Authors"); // Get all Categories Id in DropDownList Categories = GetAllCategories(); ViewBag.Categories = Categories; //ViewData["CatId"] = new SelectList(_context.Categories, "CatId", "Categories"); // Get all Categories Id in DropDownList Publishers = GetAllPublishers(); ViewBag.Publishers = Publishers; //ViewData["PubId"] = new SelectList(_context.Publishers, "PubId", "Publishers"); return View(titles); } // GET: Titles/Edit/5 [Authorize(Roles = "admin")] public IActionResult Edit(int? id) { if (id == null) { return HttpNotFound(); } Titles titles = _context.Titles.Single(m => m.TitleId == id); if (titles == null) { return HttpNotFound(); } // Get all Authors Id in DropDownList Authors = GetAllAuthors(); ViewBag.Authors = Authors; //ViewData["AuthorId"] = new SelectList(_context.Authors, "AuthorId", "Authors"); // Get all Categories Id in DropDownList Categories = GetAllCategories(); ViewBag.Categories = Categories; //ViewData["CatId"] = new SelectList(_context.Categories, "CatId", "Categories"); // Get all Categories Id in DropDownList Publishers = GetAllPublishers(); ViewBag.Publishers = Publishers; //ViewData["PubId"] = new SelectList(_context.Publishers, "PubId", "Publishers"); return View(titles); } // POST: Titles/Edit/5 [HttpPost] [ValidateAntiForgeryToken] [Authorize(Roles = "admin")] public IActionResult Edit(Titles titles) { if (ModelState.IsValid) { _context.Update(titles); _context.SaveChanges(); return RedirectToAction("Index"); } // Get all Authors Id in DropDownList Authors = GetAllAuthors(); ViewBag.Authors = Authors; //ViewData["AuthorId"] = new SelectList(_context.Authors, "AuthorId", "Authors"); // Get all Categories Id in DropDownList Categories = GetAllCategories(); ViewBag.Categories = Categories; //ViewData["CatId"] = new SelectList(_context.Categories, "CatId", "Categories"); // Get all Categories Id in DropDownList Publishers = GetAllPublishers(); ViewBag.Publishers = Publishers; //ViewData["PubId"] = new SelectList(_context.Publishers, "PubId", "Publishers"); return View(titles); } // GET: Titles/Delete/5 [ActionName("Delete")] [Authorize(Roles = "admin")] public IActionResult Delete(int? id) { if (id == null) { return HttpNotFound(); } Titles titles = _context.Titles.Single(m => m.TitleId == id); if (titles == null) { return HttpNotFound(); } return View(titles); } // POST: Titles/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] [Authorize(Roles = "admin")] public IActionResult DeleteConfirmed(int id) { Titles titles = _context.Titles.Single(m => m.TitleId == id); _context.Titles.Remove(titles); _context.SaveChanges(); return RedirectToAction("Index"); } }
Then, from View Page of the Input DropDownList
<form asp-controller="Titles" asp-action="Create" method="POST"><div class="form-horizontal"><h4>Titles</h4><hr /><div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div><div class="form-group"><label asp-for="AuthorId" class="col-md-2 control-label"></label><div class="col-md-10"><select asp-for="AuthorId" asp-items="@ViewBag.Authors" class="form-control"></select></div></div><div class="form-group"><label asp-for="BookCode" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="BookCode" class="form-control" /><span asp-validation-for="BookCode" class="text-danger" /></div></div><div class="form-group"><label asp-for="BookTitle" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="BookTitle" class="form-control" /><span asp-validation-for="BookTitle" class="text-danger" /></div></div><div class="form-group"><label asp-for="CatId" class="col-md-2 control-label"></label><div class="col-md-10"><select asp-for="CatId" asp-items="@ViewBag.Categories" class="form-control"></select></div></div><div class="form-group"><label asp-for="Notes" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="Notes" class="form-control" /><span asp-validation-for="Notes" class="text-danger" /></div></div><div class="form-group"><label asp-for="Price" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="Price" class="form-control" /><span asp-validation-for="Price" class="text-danger" /></div></div><div class="form-group"><label asp-for="PubDate" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="PubDate" class="form-control" /><span asp-validation-for="PubDate" class="text-danger" /></div></div><div class="form-group"><label asp-for="PubId" class="col-md-2 control-label"></label><div class="col-md-10"><select asp-for="PubId" asp-items="@ViewBag.Publishers" class="form-control"></select></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>
I want to use ViewData instead of ViewBag, but it does not work. Do you know how to modify this issue? Code as enjoying, thanks.