Here is the working code and implemented using model class and ViewBag to populate the DropDownList.
public class DLVendorManagement : IDLVendorManagement
{
VendorDBContext vDetailContext = new VendorDBContext();
public void InsertVendor(Vendor vendor)
{
try
{
vDetailContext.Vendors.Add(vendor);
vDetailContext.SaveChanges();
}
catch (Exception ex)
{
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataLayer.Model
{
[Table("Vendors")]
public class Vendor
{
[Key]
public int Id { get; set; }
public string Category { get; set; }
public string Description { get; set; }
public string Name { get; set; }
//public List<VendorDisplay> VendorDisplays { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataLayer.Model
{
[Table("VendorDisplays")]
public class VendorDisplay
{
[Key]
public int CId { get; set; }
public string CName { get; set; }
}
}
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataLayer.Model
{
public class VendorDBContext: DbContext
{
public DbSet<Vendor> Vendors { get; set; }
public DbSet<VendorDisplay> vendorDisplays { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=DESKTOP-1M2N4KR\\EMMA;Database=Rocky;Integrated Security=true; TrustServerCertificate=True;");
}
base.OnConfiguring(optionsBuilder);
}
}
}
using DataLayer.Model;
using DataLayer.Repository;
using Microsoft.AspNetCore.Mvc;
namespace Vendor_Management.Controllers
{
public class VendorDtrController : Controller
{
DLVendorManagement dtt = new DLVendorManagement();
public IActionResult Index()
{
List<Vendor> lstcn = dtt.GetVendors();
return View(lstcn);
}
public IActionResult Register()
{
List<VendorDisplay> vendorDisplays = dtt.GetVendorDisplay();
ViewBag.VendorDisplay = vendorDisplays;
return View();
}
[HttpPost]
public IActionResult Register(Vendor vendor)
{
dtt.InsertVendor(vendor);
List<VendorDisplay> vendorDisplays = dtt.GetVendorDisplay();
ViewBag.VendorDisplay = vendorDisplays;
ViewData["Message"] = "Record submitted successfully.";
return RedirectToAction("Register");
}
}
}
@model IEnumerable<DataLayer.Model.Vendor>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
</head>
<body>
<p>
<a asp-action="Register">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Category)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</tbody>
</table>
</body>
</html>
@model DataLayer.Model.Vendor
@{
ViewData["Title"] = "Register";
Layout = "~/Views/Shared/_Layout1.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Register</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
</head>
<body class="container">
<h4>Vendor</h4>
<hr />
<form asp-action="Register">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label asp-for="Description" class="control-label"></label>
<input asp-for="Description" class="form-control" />
@* <span asp-validation-for="Description" class="text-danger"></span> *@
</div>
<div class="form-group">
<label asp-for="Category" class="control-label"></label>
<input asp-for="Category" class="form-control" />
@* <span asp-validation-for="Category" class="text-danger"></span> *@
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label asp-for="Name" class="control-label"></label><br />
@Html.DropDownList("Name", new SelectList(ViewBag.VendorDisplay, "CId", "CName"), "Please select", new { @id = "ddlNames", @class = "form-control" })
@* <span asp-validation-for="Name" class="text-danger"></span> *@
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group text-center">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</div>
</div>
</div>
</form>
<div>
<a asp-action="Index">Back to List</a>
</div>
@if (ViewData["Message"] != null)
{
<script type="text/javascript">
window.onload = function () {
alert("@ViewData["Message"]");
};
</script>
}
</body>
</html>