Powered by AL
User
Write code of model view-model, controller, view, and startup.es for updating a department(id, name, category,
description) in ASP.NET Core.
Bing
Sure, here is a simple example of how you can update a department in ASP.NET Core.
Model (Department.cs):
public class Departrent
{
public int Id { get; sets }
public string Name { get} set; )
public string Category { get; sets }
public string Description { get; set; }
x
‘ViewModel (Department ViewModeL.cs):
public class DepartmentViewtodel
t
Public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Category { gets sets }
public string Description { get; set; }
x
Controller (DepartmentController.e:
public class DepartmentController : Controller
{
private readonly ApplicationDacontext _context;
public DepartnentController(ApplicationDbcontext context)
«
_context = context;
,
[Httpset]
public async TaskeIActionResult> Edit(int? id)
if (id == null)
{
return NotFound();
}
var department = await _context.Departments.FindAsync(id) ;
if (department == null)
t
return NotFound() 5
}
return View(department) ;
y
[HttpPost]
[validateAntiForgeryToken]
Public async Task Edit(int id, [Bind("Id,Nane,Category,Description")] Department department)
if (id != department .14)
{
return NotFound() 5+
Af (Modelstate. IsValid)
{
try
t
_context.Update (department) ;
await _context .SavechangesAsync();
catch (DoUpdateconcurrencyexception)
{
if (IDepartmentexists (department .1d))
«
return NotFound();
x
else
{
throws
y
x
return RedirectToaction(nameof(Index));
>
return View(departnent);
y
private bool Departnentéxists(int id)
{
return _context.Departments.Any(e => e.td == id);
y
x
View (Edit.eshtm):
@node Department