o Untuk membuat projek MVC menggunakan Visual Studio 2019, Create new project >
pilih ASP NET WebApplication ( NET Framework) > simpan nama project sesuai Act >
pilih template yang MVC > lalu create
o Buat class model dengan cara klik kanan pada folder model > add > class
o Buat file stylesheet dengan cara klik kanan pada nama projek “WebApplication3” > add
> StyleSheet > beri nama sesuai video BAB 8.mp4
o Buat Database dan tabel dahulu seperti pada video BAB 8.mp4, gunakan XAMPP saja.
o Untuk controller HomeController.cs, silahkan buat baru atau menggunakan yang sudah
ada pada folder Controller. Ikuti kode program HomeController.cs yang ada di bawah ini
o Tambahkan view dengan klik kanan pada ActionResult ‘Index’ pada kode program
HomeController.cs > add view > beri nama file nya > Pilih template Empty > Pilih model
class yang class1 Index.cshtml seperti pada video BAB 8.mp4. Lalukan hal yang sama
untuk membuat view Admin.cshtml , About.cshtml, dan Contact.cshtml
Kode program ada di bawah ini.
Kode Program Activity Pert 8
Note : (Inisialisasi koneksi database MySql menyesuaikan username dan password xampp anda.
Disini dicontohkan username : root , password : [kosong])
o Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
namespace WebApplication3.Models
{
public class Class1
{
public String usr, ps, stat;
public String id { get; set; }
public String pass { get; set; }
public String title { get; set; }
public String konten { get; set; }
public String hapus { get; set; }
public String time { get; set; }
o StyleSheet1.cs
ul#menu li {
display: inline;
padding-left: 20%;
text-align: center;
font-family: 'Tempus Sans ITC';
}
#border {
float: left;
margin-left: 10%;
width: 85%;
border-bottom: 2px solid #f51c40;
}
a {
font-style: normal;
color: #FF0066;
border: solid;
border-width: medium;
}
td#log {
border-right: solid;
border-width: thin;
border-color: #f51c40;
}
#tbl {
float: left;
margin-right: 5%;
margin-left: 5%;
}
#ttg {
text-align: justify;
}
#form1 {
padding-left: 10px;
}
o HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication3.Controllers
{
public class HomeController : Controller
{
//
// GET: /Controller1/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Models.Class1 mod)
{
mod.Login();
if (mod.id.Equals(mod.usr) && (mod.pass.Equals(mod.ps)))
{
HttpCookie cookie = new HttpCookie("Admin");
cookie.Value = "Hello Cookie! CreatedOn: " +
DateTime.Now.ToShortTimeString();
cookie.Expires = DateTime.Now.AddMinutes(2);
this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
return RedirectToAction("Admin", "Home");
}
return View();
}
public ActionResult Admin()
{
if
(!this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("Admin"))
{
return RedirectToAction("Index", "Home");
}
return View();
}
[HttpPost]
public ActionResult Admin(Models.Class1 cs)
{
if (cs.title != null)
{
cs.isi_kontent();
}
if (cs.hapus != null)
{
cs.hapus_kontent();
}
return View();
}
public ActionResult About()
{
return View();
}
[HttpPost]
public ActionResult About(Models.Class1 mod)
{
mod.Login();
if (mod.id.Equals(mod.usr) && (mod.pass.Equals(mod.ps)))
{
HttpCookie cookie = new HttpCookie("Admin");
cookie.Value = "Hello Cookie! CreatedOn: " +
DateTime.Now.ToShortTimeString();
cookie.Expires = DateTime.Now.AddMinutes(2);
this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
return RedirectToAction("Admin", "Home");
}
return View();
}
public ActionResult Contact()
{
return View();
}
[HttpPost]
public ActionResult Contact(Models.Class1 mod)
{
mod.Login();
if (mod.id.Equals(mod.usr) && (mod.pass.Equals(mod.ps)))
{
HttpCookie cookie = new HttpCookie("Admin");
cookie.Value = "Hello Cookie! CreatedOn: " +
DateTime.Now.ToShortTimeString();
cookie.Expires = DateTime.Now.AddMinutes(2);
this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
return RedirectToAction("Admin", "Home");
}
return View();
}
}
}
o Index.cshtml
@model WebApplication3.Models.Class1
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/StyleSheet1.css">
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table id="layout" style="width:100%;">
<tr>
<td colspan="2">
<div id="judul">
<ul id="menu">
<li>
<a href="/Home/Index">Home</a>
</li>
<li>
<a href="/Home/About">About</a>
</li>
<li>
<a href="/Home/Contact">Contact</a>
</li>
</ul>
</div>
<div id="border">
</div>
</td>
</tr>
<tr>
<td id="log" style="width: 20%;">
<form id="form1" method="post">
<h2>Admin Login</h2>
<label>Username</label>
<input type="text" id="id" name="id" />
<br /><br />
<label>Password</label>
<input type="password" id="pass" name="pass" />
<br /><br />
<input type="submit" value="login" />
</form>
</td>
<td>
<table id="tbl" border="0">
@{
WebApplication3.Models.Class1 m = new
WebApplication3.Models.Class1();
m.ambil_konten();
for (int i = 1; i <= m.judul.Count; i++)
{
String jdl = m.judul.Pop();
String isi = m.isi.Pop();
String tgl = m.tgl.Pop();
WriteLiteral("<tr><td id ='tulisan' ><div ><article><h1>"
+ jdl + "</h1><h3>" + tgl + "</h3><p id='ttg'>" + isi +
"</p></article></div></td></tr>");
}
}
</table>
</td>
</tr>
</table>
</body>
</html>
o Admin.cshtml
@model WebApplication3.Models.Class1
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/StyleSheet1.css">
<meta name="viewport" content="width=device-width" />
<title>Admin</title>
</head>
<body>
<table id="layout" style="width:100%;">
<tr>
<td colspan="2">
<div id="judul">
<ul id="menu">
<li>
<a href="/Home/Index">Home</a>
</li>
<li>
<a href="/Home/About">About</a>
</li>
<li>
<a href="/Home/Contact">Contact</a>
</li>
</ul>
</div>
<div id="border">
</div>
</td>
</tr>
<tr>
<td id="log" style="width: 20%;">
</td>
<td>
<table id="tbl" border="0">
<tr>
<td>
<form id="posting" method="post">
<table id="postingan">
<tr>
<td colspan="2"><h1>Form Admin Editing Blog
Content</h1></td>
</tr>
<tr>
<td>
<label>Judul</label>
</td>
<td>
<input type="text" id="title" name="title"
/>
</td>
</tr>
<tr>
<td>
<label>Tanggal Waktu</label>
</td>
<td>
<input type="text" value="@DateTime.Now;"
name="time" id="time" />
</td>
</tr>
<tr>
<td>
<label>Teks</label>
</td>
<td>
<textarea cols="60" rows="10" id="konten"
name="konten"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="post"
style="width:100px;height:40px;" />
</td>
</tr>
<tr>
<td>
<h3>Delete Post From blog</h3>
</td>
</tr>
<tr>
<td>
<label>Judul</label>
</td>
<td>
<input type="text" id="hapus" name="hapus"
/>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Delete Post"
style="width:100px;height:40px;" />
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
o About.cshtml
@model WebApplication3.Models.Class1
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/StyleSheet1.css">
<meta name="viewport" content="width=device-width" />
<title>About</title>
</head>
<body>
<table id="layout" style="width:100%;">
<tr>
<td colspan="2">
<div id="judul">
<ul id="menu">
<li>
<a href="/Home/Index">Home</a>
</li>
<li>
<a href="/Home/About">About</a>
</li>
<li>
<a href="/Home/Contact">Contact</a>
</li>
</ul>
</div>
<div id="border">
</div>
</td>
</tr>
<tr>
<td id="log" style="width: 20%;">
<form id="form1" method="post">
<h2>Admin Login</h2>
<label>Username</label>
<input type="text" id="id" name="id" />
<br /><br />
<label>Password</label>
<input type="password" id="pass" name="pass" />
<br /><br />
<input type="submit" value="login" />
</form>
</td>
<td>
<table id="tbl" border="0">
<tr>
<td>
<h3>This Blog was created by Hinotamashi</h3>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
o Contact.cshtml
@model WebApplication3.Models.Class1
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="/StyleSheet1.css">
<meta name="viewport" content="width=device-width" />
<title>Contact</title>
</head>
<body>
<table id="layout" style="width:100%;">
<tr>
<td colspan="2">
<div id="judul">
<ul id="menu">
<li>
<a href="/Home/Index">Home</a>
</li>
<li>
<a href="/Home/About">About</a>
</li>
<li>
<a href="/Home/Contact">Contact</a>
</li>
</ul>
</div>
<div id="border">
</div>
</td>
</tr>
<tr>
<td id="log" style="width: 20%;">
<form id="form1" method="post">
<h2>Admin Login</h2>
<label>Username</label>
<input type="text" id="id" name="id" />
<br /><br />
<label>Password</label>
<input type="password" id="pass" name="pass" />
<br /><br />
<input type="submit" value="login" />
</form>
</td>
<td>
<table id="tbl" border="0">
<tr>
<td>
<h1>
Contact Us
</h1>
<br />
<p>
Email: hinotamashi94@gmail.com
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
Contoh Output
(Login admin menggunakan username : root , password : admin)