Hi Guys,
When user click Add succesfully inserted data into database
I trying to show back data on another view not in Index.cshtml
I want show data in Create.cshtml with model PinjamanModel as the name.
And then I will show back data from database in Create.cshtml from model Detail_Pinjaman_Buku looping in table.
I'm using Viewbag for looping data on table.
When I tried it I'm get error
NullReferenceException: Object reference not set to an instance of an object."
Any help could be apriciate.
Create.cshtml
@using www.si.perpustakaan.Models
@model PinjamanModel
@{
ViewData["Title"] = "Create";
Layout = "~/Views/Shared/AdminDashboard/_Layout.cshtml";
}
<script src="~/lib/jquery/dist/jquery.3.3.1.min.js"></script>
<script src="~/js/json2.js"></script>
<link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
<script src="~/assets/plugins/jquery-ui/jquery-ui.js"></script>
<link href="~/assets/plugins/jquery-ui/jquery-ui.css" rel="stylesheet" />
<div class="tab-content">
<div class="tab-pane tabs-animation fade show active" id="tab-content-0" role="tabpanel">
<div class="row">
<div class="col-md-12">
<div class="main-card mb-3 card">
<div class="card-body">
<!-- Modal Header -->
<div class="modal-header bg bg-success">
<div align="center">
<h4 class="modal-title"><b>Add Data Pinjaman</b></h4>
</div>
</div>
<hr />
<form asp-action="" asp-controller="" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="row">
<div class="col-md-6">
<div class="position-relative form-group">
<label asp-for="Kode_Pinjam" class="control-label"></label>
<input asp-for="Kode_Pinjam" id="Kode_Pinjam" name="Kode_Pinjam" class="form-control" readonly="readonly" />
<span asp-validation-for="Kode_Pinjam" class="text-danger"></span>
</div>
</div>
<div class="col-md-6">
<div class="position-relative form-group">
<label asp-for="Tanggal_Pinjam" class="control-label"></label>
<input asp-for="Tanggal_Pinjam" class="form-control" placeholder="Enter Tanggal Pinjam Buku" />
<span asp-validation-for="Tanggal_Pinjam" class="text-danger"></span>
</div>
</div>
<div class="col-md-6">
<div class="position-relative form-group">
<label asp-for="Tanggal_Kembali" class="control-label"></label>
<input asp-for="Tanggal_Kembali" class="form-control" placeholder="Enter Tanggal Kembali Buku" />
<span asp-validation-for="Tanggal_Kembali" class="text-danger"></span>
</div>
</div>
<div class="col-md-6">
<div class="position-relative form-group">
<label asp-for="Kode_Anggota" class="control-label"></label>
<div class="input-group">
<input asp-for="Kode_Anggota" class="form-control" id="Kode_Anggota" placeholder="Enter Kode Anggota" required />
<div class="input-group-append">
<button type="button" id="CekNamaAnggota" class="btn btn-warning">Cari Kode Anggota</button>
</div>
</div>
<span asp-validation-for="Kode_Anggota" class="text-danger"></span>
</div>
</div>
<div class="col-md-6">
<div class="position-relative form-group">
<label asp-for="Nama_Anggota" class="control-label"></label>
<input asp-for="Nama_Anggota" class="form-control" id="Nama_Anggota" placeholder="Enter Nama Anggota" readonly="readonly" />
</div>
<span asp-validation-for="Nama_Anggota" class="text-danger"></span>
</div>
<div class="col-md-6">
<div class="position-relative form-group">
<label asp-for="Kode_Petugas" class="control-label"></label>
<div class="input-group">
<input asp-for="Kode_Petugas" class="form-control" id="Kode_Petugas" placeholder="Enter Kode Petugas" required />
<div class="input-group-append">
<button type="button" id="CekNamaPetugas" class="btn btn-warning">Cari Kode Petugas</button>
</div>
</div>
<span asp-validation-for="Kode_Petugas" class="text-danger"></span>
</div>
</div>
<div class="col-md-6">
<div class="position-relative form-group">
<label asp-for="Nama_Petugas" class="control-label"></label>
<input asp-for="Nama_Petugas" class="form-control" id="Nama_Petugas" placeholder="Enter Nama Petugas" readonly="readonly" />
</div>
<span asp-validation-for="Nama_Petugas" class="text-danger"></span>
</div>
@*Data Buku*@
<div class="col-md-12">
<div class="main-card mb-3 card">
<div class="card-body">
<!-- Modal Header -->
<div class="modal-header bg bg-primary">
<div align="center">
<h4 class="modal-title"><b>Data Buku</b></h4>
</div>
</div>
<hr />
<table>
<thead>
<tr>
<th>Kode Buku</th>
<th>Judul</th>
<th>Penulis</th>
<th>Penerbit</th>
</tr>
</thead>
<tbody>
<tr>
<td><input asp-for="Kode_Buku" class="form-control" id="Kode_Buku" placeholder="Enter Kode Buku" name="Kode_Buku" required /></td>
<td><input asp-for="Judul" class="form-control" id="Judul" placeholder="Enter Judul Buku" name="Judul" readonly="readonly" /></td>
<td><input asp-for="Penulis" class="form-control" id="Penulis" placeholder="Enter Penulis" name="Penulis" readonly="readonly" /></td>
<td><input asp-for="Penerbit" class="form-control" id="Penerbit" placeholder="Enter Penerbit" name="Penerbit" readonly="readonly" /></td>
<td><button type="button" id="Btn_Search_Kode_Buku" class="btn btn-warning btn-sm"><i class="fa fa-search"> Cari</i></button></td>
<td><button type="button" id="Btn_Add_Search_Kode_Buku" class="btn btn-success btn-sm"><i class="fa fa-plus"> Add</i></button></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
@*Get error when looping in this table. Data will show back on this table
This data come from Det Detail_Pinjaman_Buku*@
<table>
<thead class="bg bg-dark text-center align-middle text-white">
<tr>
<th>Kode Buku</th>
<th>Penulis</th>
<th>Penerbit</th>
</tr>
</thead>
<tbody>
@if (Model != null)
{
@foreach (Detail_Pinjaman_Buku pinjaman_Buku in ViewBag.DetailPinjamanBuku)
{
<tr>
<td>@pinjaman_Buku.Kode_Buku</td>
<td>@pinjaman_Buku.Judul</td>
<td>@pinjaman_Buku.Penulis</td>
<td>@pinjaman_Buku.Penerbit</td>
</tr>
}
}
</tbody>
</table>
<div class="col-md-12">
<div class="form-group">
<div align="center">
@*<input type="submit" value="Save Data Pinjaman" class="btn btn-success" />*@
</div>
</div>
<div>
<a asp-action="Index"><i class="fa fa-arrow-alt-circle-left"> Back to List</i></a>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#Btn_Add_Search_Kode_Buku').on("click", function () {
var ObjectBuku = {
Kode_Pinjam: $("#Kode_Pinjam").val(),
Kode_Buku: $("#Kode_Buku").val(),
Judul: $("#Judul").val(),
Penulis: $("#Penulis").val(),
Penerbit: $("#Penerbit").val()
};
$.ajax({
url: "/Pinjaman/SaveData_Search_Buku",
data: JSON.stringify(ObjectBuku),
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
window.alert("Data berhasil disimpan..");
LoadDataPinjaman();
},
error: function (errormessage) {
window.alert(errormessage.responseText);
}
});
return false;
});
});
</script>
<script type="text/javascript" lang="javascript">
function LoadDataPinjaman() {
$.ajax({
url: "@Url.Action("LoadData_Buku", "Pinjaman")",
type: "GET",
contentType: "application/json;charset=UTF-8",
dataType: "json",
data: { Kode_Pinjam: $('#Kode_Pinjam').val() },
});
}
</script>
<script type="text/javascript" lang="javascript">
$(document).ready(function () {
$('#Btn_Search_Kode_Buku').on("click", function () {
$.ajax({
url: "@Url.Action("Get_DataBukuByID", "Pinjaman")",
type: "GET",
contentType: "application/json;charset=UTF-8",
dataType: "json",
data: { Kode_Buku: $('#Kode_Buku').val() },
success: function (data) {
if (data !="") {
$('#Judul').val(data.Judul);
$('#Penulis').val(data.Nama_Penulis);
$('#Penerbit').val(data.Penerbit);
}
else {
window.alert(' error : ' + data.message);
}
}
});
});
return false;
});
</script>
<script type="text/javascript" lang="javascript">
$(document).ready(function () {
$("#CekNamaAnggota").click(function () {
$.ajax({
url: "@Url.Action("SearchNamaAnggota", "Pinjaman")",
type:"POST",
data: { Kode_Anggota: $("#Kode_Anggota").val() },
success: function (data) {
if (data != "") {
$("#Nama_Anggota").val(data);
}
else {
alert("Kode Anggota tidak ditemukan !!!");
$("#Kode_Anggota").val("");
$("#Nama_Anggota").val("");
}
}
});
});
});
</script>
<script type="text/javascript" lang="javascript">
$(document).ready(function () {
$("#CekNamaPetugas").click(function () {
$.ajax({
url: "@Url.Action("SearchNamaPetugas", "Pinjaman")",
type:"POST",
data: { Kode_Petugas: $("#Kode_Petugas").val() },
success: function (data) {
if (data != "")
{
$("#Nama_Petugas").val(data);
}
else
{
alert("Kode Petugas tidak ditemukan !!!");
$("#Kode_Petugas").val("");
$("#Nama_Petugas").val("");
}
}
});
});
});
</script>
<script type="text/javascript" lang="javascript">
$("[id*=Tanggal_Pinjam]").attr("ReadOnly", true)
$("[id*=Tanggal_Pinjam]").datepicker(
{
dateFormat: "yy/mm/dd"
});
$("[id*=Tanggal_Kembali]").attr("ReadOnly", true)
$("[id*=Tanggal_Kembali]").datepicker(
{
dateFormat: "yy/mm/dd"
});
</script>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Controller.cs
[HttpGet]
public JsonResult LoadData_Buku(string Kode_Pinjam)
{
ViewBag.DetailPinjamanBuku = GetAllPinjamanBuku(Kode_Pinjam);
return Json(Kode_Pinjam);
}
public List<Detail_Pinjaman_Buku> GetAllPinjamanBuku(string Kode_Pinjam)
{
List<Detail_Pinjaman_Buku> detail_Pinjaman_Buku = new List<Detail_Pinjaman_Buku>();
using (SqlConnection con = new SqlConnection(this._configuration.GetConnectionString("db_perpustakaan")))
{
using(SqlCommand cmd = new SqlCommand())
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select Kode_Buku, Judul, Penulis, Penerbit From Detail_Pinjaman Where Kode_Pinjam=@Kode_Pinjam";
SqlParameter parameter = new SqlParameter("@Kode_Pinjam", Kode_Pinjam);
cmd.Parameters.Add(parameter);
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
detail_Pinjaman_Buku.Add(new Detail_Pinjaman_Buku
{
Kode_Buku = rdr["Kode_Buku"].ToString(),
Judul = rdr["Judul"].ToString(),
Penulis = rdr["Penulis"].ToString(),
Penerbit = rdr["Penerbit"].ToString(),
});
}
}
}
}
return detail_Pinjaman_Buku;
}