I have 2 model class with the same id field namely vendors and vendor display.
The DropDownList (name) is populated from the vendor display model.
Now what i want is when i select a text from the DropDownList, it should use its selected value to delete record from the vendors class because the id in the vendors model is the same thing as the vendor display model
Database
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; }
}
}
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("VendorDisplays")]
public class VendorDisplay
{
[Key]
public int CId { get; set; }
public string CName { 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;
}
}
}
Error
ArgumentNullException: Value cannot be null. (Parameter 'items')
System.ArgumentNullException.Throw(string paramName)
CallSite.Target(Closure , CallSite , Type , object , string , string )
System.Dynamic.UpdateDelegates.UpdateAndExecute4<T0, T1, T2, T3, TRet>(CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
AspNetCoreGeneratedDocument.Views_VendorDtr_Delete.<ExecuteAsync>b__11_2() in Delete.cshtml
+
@Html.DropDownListFor(m => m.CId, new SelectList(ViewBag.VendorDisplay, "CId", "CName"), "Please select", new { @id = "ddlNames", @class = "form-control" })
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.GetChildContentAsync(bool useCachedResult, HtmlEncoder encoder)
Microsoft.AspNetCore.Mvc.TagHelpers.RenderAtEndOfFormTagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.<RunAsync>g__Awaited|0_0(Task task, TagHelperExecutionContext executionContext, int i, int count)
AspNetCoreGeneratedDocument.Views_VendorDtr_Delete.<ExecuteAsync>b__11_1()
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.SetOutputContentAsync()
AspNetCoreGeneratedDocument.Views_VendorDtr_Delete.ExecuteAsync() in Delete.cshtml
+
Layout = "~/Views/Shared/_Layout1.cshtml";
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|30_0<TFilter, TFilterAsync>(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
Show raw exception details