0% found this document useful (0 votes)
61 views16 pages

Pert4 - Act1 - Sheka Tri Putra Darma - 11120095

This document contains code for a web application that manages employee data. It includes: 1) A Data class to store employee information like name, joining date, and age 2) Views for creating, editing, and displaying a list of employees 3) A HomeController class with actions to handle data operations like adding, updating, and deleting employees 4) Code uses static list of sample employees and basic validation for data fields
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)
61 views16 pages

Pert4 - Act1 - Sheka Tri Putra Darma - 11120095

This document contains code for a web application that manages employee data. It includes: 1) A Data class to store employee information like name, joining date, and age 2) Views for creating, editing, and displaying a list of employees 3) A HomeController class with actions to handle data operations like adding, updating, and deleting employees 4) Code uses static list of sample employees and basic validation for data fields
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/ 16

Nama : Sheka Tri Putra Darma

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
},
};

public ActionResult Index()


{
var DataList = from e in KaryawanList
orderby e.ID
select e;
return View(DataList);

//
// GET: /Home/Details/5

public ActionResult Details(int id)


{
return View();
}

//
// GET: /Home/Create

public ActionResult Create()


{
return View();
}

//
// POST: /Home/Create

[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here

Data dt = new Data();


dt.Name = collection["Name"];
DateTime date = new DateTime();
dt.JoiningDate = Convert.ToDateTime(collection["JoiningDate"] + "
" +
date.TimeOfDay.ToString());
string age = collection["Age"];
dt.Age = Int32.Parse(age);
KaryawanList.Add(dt);
return RedirectToAction("Index");
}
catch
{
return View();
}

//
// GET: /Home/Edit/5

public ActionResult Edit(int id)


{
List<Data> data_karyawan = KaryawanList;
var pegawai = data_karyawan.Single(m => m.ID == id);
return View(pegawai);

//
// 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

public ActionResult Delete(int id)


{
List<Data> data_karyawan = KaryawanList;
KaryawanList.RemoveAt(id - 1);
return RedirectToAction("Index");

//
// 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>

@foreach (var item in Model)


{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)

</td>

<td>
@Html.DisplayFor(modelItem => item.JoiningDate)
</td>
<td>

@Html.DisplayFor(modelItem => item.Age)


</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" })

@Html.HiddenFor(model => model.ID)

<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

You might also like