Pert4 - Act1 - Sheka Tri Putra Darma - 11120095
Pert4 - Act1 - Sheka Tri Putra Darma - 11120095
NPM : 11120095
Kelas : 3KA05
Pertemuan 4
Act 4
Data.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace pert4_11120095.Models
{
public class Data
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime JoiningDate { get; set; }
public int Age { get; set; }
}
}
Create.cshtml
@model pert4_11120095.Models.Data
@{
ViewBag.Title = "Create";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width = device-width" />
<title>Create</title>
</head>
<body>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Data Karyawan</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes:
new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new
{
htmlAttributes = new { @class = "form-control" }
})
@Html.ValidationMessageFor(model => model.Name, "", new {
@class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.JoiningDate, htmlAttributes:
new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.JoiningDate, new
{
htmlAttributes = new { @class = "form-control" }
})
@Html.ValidationMessageFor(model => model.JoiningDate, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Age, htmlAttributes:
new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Age, new
{
htmlAttributes = new { @class = "form-control" }
})
@Html.ValidationMessageFor(model => model.Age, "", new
{
@class =
"text-danger"
})
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default"
/>
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>
HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using pert4_11120095.Models;
namespace pert4_11120095.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
Data isi = new Data();
List<Data> data;
public static List<Data> KaryawanList = new List<Data>
{
new Data{
ID = 1,
Name = "Allan",
JoiningDate = DateTime.Parse(DateTime.Today.ToString()), Age = 23
},
new Data{
ID = 2,
Name = "Carson",
JoiningDate = DateTime.Parse(DateTime.Today.ToString()), Age = 45
},
new Data{
ID = 3,
Name = "Carson",
JoiningDate = DateTime.Parse(DateTime.Today.ToString()), Age = 37
},
new Data{
ID = 4,
Name = "Laura",
JoiningDate = DateTime.Parse(DateTime.Today.ToString()), Age = 26
},
};
//
// GET: /Home/Details/5
//
// GET: /Home/Create
//
// POST: /Home/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
//
// GET: /Home/Edit/5
//
// POST: /Home/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
var employee = KaryawanList.Single(m => m.ID == id);
if (TryUpdateModel(employee))
{
//To Do:- database code
return RedirectToAction("Index");
}
return View(employee);
}
catch
{
return View();
}
//
// GET: /Home/Delete/5
//
// POST: /Home/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
Index.cshtml
@model IEnumerable<pert4_11120095.Models.Data>
@{
ViewBag.Title = "Index";
}
<html>
<head>
<meta name="viewport" content="width = device-width" />
<title>Index</title>
</head>
<body>
<p>@Html.ActionLink("Create New", "Create")</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.JoiningDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th></th>
</tr>
</td>
<td>
@Html.DisplayFor(modelItem => item.JoiningDate)
</td>
<td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID })
|
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
</body>
</html>
Edit.cshtml
@model pert4_11120095.Models.Data
@{
ViewBag.Title = "Edit";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width = device-width" />
<title>Edit</title>
</head>
<body>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Data Karyawan</h4>
<hr />
@Html.ValidationSummary(
true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(
model => model.Name, htmlAttributes: new
{
@class = "control-label col-md-2"
})
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new
{
htmlAttributes = new
{
@class = "form-control"
}
})
@Html.ValidationMessageFor(model => model.Name, "", new
{
@class = "text-danger"
})
</div>
</div>
<div class="form-group">
@Html.LabelFor(
model => model.JoiningDate, htmlAttributes: new
{
@class = "control-label col-md-2"
})
<div class="col-md-10">
@Html.EditorFor(
model => model.JoiningDate, new
{
htmlAttributes = new { @class = "form-control" }
})
@Html.ValidationMessageFor(
model => model.JoiningDate, "", new
{
@class = "text-danger"
})
</div>
</div>
<div class="form-group">
@Html.LabelFor(
model => model.Age, htmlAttributes: new
{
@class = "control-label col-md-2"
})
<div class="col-md-10">
@Html.EditorFor(
model => model.Age, new
{
htmlAttributes = new { @class = "form-control" }
})
@Html.ValidationMessageFor(
model => model.Age, "", new
{
@class = "text-danger"
})
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>
Output