I HAVE 2 MODEL CLASS WITH THE SAME ID FIELD NAMELY VENDORS AND VENDOR DISPALY.
THE DROPDOWNLIST(NAME) IS POPULATED FROM THE VENDOR DISPLAY MODEL.
NOW WHAT I WANT IS WHEN I SELECT A TEXT FROM THE DROPDOWNLIST, IT SHOUD USE THE VALUE TO READ THE RECORD FROM THE VENDORS CLASS BECAUSE THE ID IN THE VENDORS MODEL IS THE SAME THING AS THE VENDOR DISPLAY MODEL.
MY CODE
USE [Rocky]
GO
/****** Object: Table [dbo].[VendorDisplays] Script Date: 5/8/2024 1:33:39 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[VendorDisplays](
[CId] [int] IDENTITY(1,1) NOT NULL,
[CName] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_VendorDisplays] PRIMARY KEY CLUSTERED
(
[CId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object: Table [dbo].[Vendors] Script Date: 5/8/2024 1:33:40 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Vendors](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Category] [nvarchar](max) NOT NULL,
[Description] [nvarchar](max) NOT NULL,
[Name] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_Vendors] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
MODEL CLASS
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; }
}
}
REPOSITORY
using DataLayer.Abstract;
using DataLayer.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataLayer.Repository
{
public class DLVendorManagement : IDLVendorManagement
{
VendorDBContext vDetailContext = new VendorDBContext();
public void InsertVendor(Vendor vendor)
{
try
{
vDetailContext.Vendors.Add(vendor);
vDetailContext.SaveChanges();
}
catch (Exception ex)
{
}
}
public void UpdateVendor(Vendor vendor)
{
try
{
Vendor vendor1 = vDetailContext.Vendors.Where(x => x.Id == vendor.Id).FirstOrDefault();
vendor1.Description = vendor.Description;
vendor1.Category = vendor.Category;
vendor1.Name = vendor.Name;
vDetailContext.SaveChanges();
}
catch (Exception ex)
{
}
}
public void DeleteVendor(VendorDisplay vd)
{
try
{
Vendor vendor1 = vDetailContext.Vendors.Where(x => x.Name.Equals(vd.CId)).FirstOrDefault();
vDetailContext.Vendors.Remove(vendor1);
vDetailContext.SaveChanges();
}
catch (Exception ex)
{
}
}
public List<Vendor> GetVendors()
{
List<Vendor> lst = new List<Vendor>();
try
{
lst = vDetailContext.Vendors.ToList();
}
catch (Exception ex)
{
}
return lst;
}
public List<VendorDisplay> GetVendorDisplay()
{
List<VendorDisplay> lst = new List<VendorDisplay>();
try
{
lst = vDetailContext.vendorDisplays.ToList();
}
catch (Exception ex)
{
}
return lst;
}
}
}
CONTROLLER
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");
}
public IActionResult Edit()
{
return View();
}
[HttpPost]
public IActionResult Edit(Vendor vendor)
{
return View();
}
public IActionResult Details()
{
List<Vendor> lstcn = dtt.GetVendors();
return View(lstcn);
}
public IActionResult Delete()
{
ViewBag.VendorDisplay = dtt.GetVendorDisplay();
return View();
}
[HttpPost]
public IActionResult Delete(VendorDisplay vd)
{
dtt.DeleteVendor(vd);
return View();
}
public List<VendorDisplay> GetVendorDetails()
{
VendorDBContext vDetailContext = new VendorDBContext();
// I have hardcoded Role data. You can collect it from database
List<VendorDisplay> lst = new List<VendorDisplay>();
try
{
lst = vDetailContext.vendorDisplays.ToList();
}
catch (Exception ex)
{
}
return lst;
}
}
}
INDEX PAGE
@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"] = "Edit";
Layout = "~/Views/Shared/_Layout1.cshtml";
}
<h1>Edit</h1>
<h4>Vendor</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Id" class="control-label"></label>
<input asp-for="Id" class="form-control" />
<span asp-validation-for="Id" 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 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="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>