0% found this document useful (0 votes)
15 views

Name: Ahmad Jarir at Thobari Class: 4 D4 IT A NRP: 2110171010

Uploaded by

Ahmad Jarir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Name: Ahmad Jarir at Thobari Class: 4 D4 IT A NRP: 2110171010

Uploaded by

Ahmad Jarir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Name : Ahmad Jarir At Thobari

Class : 4 D4 IT A

NRP : 2110171010

Source Code

UnitModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace TestWebApp.Models
{
public class UnitModel
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public int Status { get; set; }
public DateTime CreatedAt { get; set; }
}
}

UnitController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using TestWebApp.Data;
using TestWebApp.Models;

namespace TestWebApp.Controllers
{
public class UnitController : Controller
{
private readonly ApplicationDbContext _context;

public UnitController(ApplicationDbContext context)


{
_context = context;
}

// GET: Unit
public async Task<IActionResult> Index()
{
return View(await _context.units.ToListAsync());
}

// GET: Unit/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}

var unitModel = await _context.units


.FirstOrDefaultAsync(m => m.Id == id);
if (unitModel == null)
{
return NotFound();
}
return View(unitModel);
}

// GET: Unit/Create
public IActionResult Create()
{
return View();
}

// POST: Unit/Create
// To protect from overposting attacks, enable the specific properties you
want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Name,Status")] UnitModel
unitModel)
{
if (ModelState.IsValid)
{
unitModel.CreatedAt = DateTime.Now;
_context.Add(unitModel);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(unitModel);
}

// GET: Unit/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var unitModel = await _context.units.FindAsync(id);
if (unitModel == null)
{
return NotFound();
}
return View(unitModel);
}

// POST: Unit/Edit/5
// To protect from overposting attacks, enable the specific properties you
want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Name,Status")]
UnitModel unitModel)
{
if (id != unitModel.Id)
{
return Content("not found" + id + " " + unitModel.Id);
//return NotFound();
}

if (ModelState.IsValid)
{
try
{
_context.Update(unitModel);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!UnitModelExists(unitModel.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(unitModel);
}

// GET: Unit/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}

var unitModel = await _context.units


.FirstOrDefaultAsync(m => m.Id == id);
if (unitModel == null)
{
return NotFound();
}

return View(unitModel);
}

// POST: Unit/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var unitModel = await _context.units.FindAsync(id);
_context.units.Remove(unitModel);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}

private bool UnitModelExists(int id)


{
return _context.units.Any(e => e.Id == id);
}
}
}

Index.cshtml

@model IEnumerable<TestWebApp.Models.UnitModel>

@{
ViewData["Title"] = "Index";
}

<h1>Index</h1>

<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Status)
</th>
<th>
@Html.DisplayNameFor(model => model.CreatedAt)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Status)
</td>
<td>
@Html.DisplayFor(modelItem => item.CreatedAt)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>

Create.cshtml

@model TestWebApp.Models.UnitModel

@{
ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>UnitModel</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<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="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">
<label asp-for="Status" class="control-label"></label>
<input asp-for="Status" class="form-control" />
<span asp-validation-for="Status" class="text-danger"></span>
</div>
@*<div class="form-group">
<label asp-for="CreatedAt" class="control-label"></label>
<input asp-for="CreatedAt" class="form-control" />
<span asp-validation-for="CreatedAt" class="text-danger"></span>
</div>*@
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>

<div>
<a asp-action="Index">Back to List</a>
</div>

@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Edit.cshtml

@model TestWebApp.Models.UnitModel

@{
ViewData["Title"] = "Edit";
}

<h1>Edit</h1>

<h4>UnitModel</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Id" />
<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">
<label asp-for="Status" class="control-label"></label>
<input asp-for="Status" class="form-control" />
<span asp-validation-for="Status" class="text-danger"></span>
</div>
@*<div class="form-group">
<label asp-for="CreatedAt" class="control-label"></label>
<input asp-for="CreatedAt" class="form-control" />
<span asp-validation-for="CreatedAt" 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>

@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Delete.cshtml

@model TestWebApp.Models.UnitModel

@{
ViewData["Title"] = "Delete";
}

<h1>Delete</h1>

<h3>Are you sure you want to delete this?</h3>


<div>
<h4>UnitModel</h4>
<hr />
<dl class="row">
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Name)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Status)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Status)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.CreatedAt)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.CreatedAt)
</dd>
</dl>

<form asp-action="Delete">
<input type="hidden" asp-for="Id" />
<input type="submit" value="Delete" class="btn btn-danger" /> |
<a asp-action="Index">Back to List</a>
</form>
</div>

Output

You might also like